Reputation: 33
Hi there this might me a very noob question but I'm stuck, and I'm trying to do this for a while, please help me.
I'm trying to do something like this:
a=0.5;
ODE= @(x,y) ((-2*(a^2)*x*y)/(x^2+y^2)^2)/(1-((a^2)*(x^2-y^2)/(x^2+y^2)^2));
%The classical RK4 solution as a function
k1 = ODE(x,y);
k2 = ODE(x+0.5*dx,y+k1*0.5*dx);
k3 = ODE(x+0.5*dx,y+k2*0.5*dx);
k4 = ODE(x+dx,y+k3*dx);
rk4= @(x,y,dx) y + 1/6*(k1 + 2*(k2+k3) + k4)*dx;
So I first define ODE as an anonymous function then I'm trying to define k's from the ODE and finally RK4 from k's.
But this doesn't work. Any suggestion what to do?
Upvotes: 1
Views: 964
Reputation: 5765
Your k1
through k4
are not being defined or used as functions. When you declare k1 = ODE(x,y)
, this tries to evaluate the ODE
function with vectors x
and y
that aren't defined. What you probably mean to say is :
k1 = @(x,y,dx) (ODE(x,y));
k2 = @(x,y,dx) (ODE(x+0.5*dx,y+k1(x,y,dx)*0.5*dx));
k3 = @(x,y,dx) (ODE(x+0.5*dx,y+k2(x,y,dx)*0.5*dx));
k4 = @(x,y,dx) (ODE(x+dx,y+k3(x,y,dx)*dx));
rk4= @(x,y,dx) (y + 1/6*(k1(x,y,dx) + \
2*(k2(x,y,dx)+k3(x,y,dx)) + \
k4(x,y,dx))*dx);
Upvotes: 2