Where in my code using runge- kutta four step method did I make a mistake?

% compute our trajectory
dt = .01;
tspan = 0:dt:4;
Y(:,1) = y0;
yk = y0;
for i=1:length(tspan)
time= i*dt;
ykplus1=rk4singlestep(@(t,y)lorenz(t,y,sigma,beta,rho),dt,time,yk);
Y =[Y ykplus1];
yk = ykplus1;
end
plot3(Y(1,:),Y(2,:),Y(3,:),'b')

7 Comments

Can you show rk4singlestep function?
The highlighted rk4singlestep says that ive used the viriable, but never set it. This is what ive done in the first editor followed by the seccond editor window.
1. function dy = lorenz(t,y,sigma,beta,rho)
% y is a three dimentional state-vector
dy = [
sigma*(y(2)-y(1));
y(1)*(rho-y(3))-y(2);
y(1)*y(2)-beta*y(3);
];
2. function yout = rk4singlestep(fun,dt,tk,yk)
f1 = fun(tk,yk);
f2 = fun(tk+dt/2,yk+(dt/2)*f1);
f3 = fun(tk+dt/2,yk+(dt/2)*f2);
f4 = fun(tk+dt,yk+dt*f3);
yout = yk + (dt/6)*(f1+2*f2+2*f3+f4);
Where are sigma, beta, and rho set? You need to do that before you use them in the function handle.
Can you post the complete error message, including the offending line?
??? Undefined variable "rk4singlestep" or class "rk4singlestep".
Is your rk4singlestep function in a file called rk4singlestep.m?
?? I don't know how to put it in a file or make one.
Thanks, but I found out that I mixed up my code and not saving the code to matlab. But I did get my answer. Thanks

Sign in to comment.

 Accepted Answer

Type the following at the command line:
edit rk4singlestep.m
Then copy your code into that file and save it.

3 Comments

Good day. When I do that it takes me straight to the code which I wrote already in the editing window.
And when i run the code it gives the same error answer.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!