Error in assignment when solving RK2
Show older comments
i keep getting error saying error of assignment, how do i fix this?
clf;
W = 80; %Forcing wind
%initial condition
theta0 = 1e-3; %initial radians
z0 = [0;0;theta0;0];
tspan = [0, 1000]; %around 16 minutes
h = 1/1000; %using the frequency as step size
t = [tspan(1):h:tspan(2)];
n = length(t);
z = z0; Z = NaN(n,4); Z(1,:) = z';
for i = 2:n
z = rk2(@(t,y) fTacoma(t,y,W), t(i),h,z);
Z(i,:) = z';
end
plot(t,Z);
function y = rk2(t,w,h)
k1 = zdot(t,z)
k2 = zdot(t+ (h/2) , w + (k1/2))
y = z + k2
return
end
Answers (1)
Walter Roberson
on 27 May 2018
Inside rk2 you have
k1 = zdot(t,z)
We do not know what zdot is; you have not defined any variable by that name, and there is no Mathworks function by that name.
If we presume that it is a function that you defined yourself, then:
you are trying to pass z to zdot. But inside rk2, z is not defined. You did define a variable named z outside of rk2, but you defined it in a script, and variables are never shared between scripts and functions that are called by the script.
4 Comments
Luna
on 28 May 2018
Edited: Walter Roberson
on 28 May 2018
Walter Roberson
on 28 May 2018
That is not a function named zdot: that is a function named fTacoma.
Your line
z = rk2(@(t,y) fTacoma(t,y,W), t(i),h,z);
invokes rk2 passing in four arguments:
- the handle to the anonymous function @(t,y) fTacoma(t,y,W)
- t(i)
- h
- z
but your function rk2 is
function y = rk2(t,w,h)
which expects t, w, h. The function handle would be passed in as t, t(i) would be passed in as w, h would be passed in as h, and the extra z passed in would trigger an error about too many parameters.
Luna
on 29 May 2018
Edited: Walter Roberson
on 29 May 2018
Walter Roberson
on 29 May 2018
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!