Error in assignment when solving RK2
이전 댓글 표시
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
답변 (1개)
Walter Roberson
2018년 5월 27일
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
Luna
2018년 5월 28일
편집: Walter Roberson
2018년 5월 28일
Walter Roberson
2018년 5월 28일
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
2018년 5월 29일
편집: Walter Roberson
2018년 5월 29일
Walter Roberson
2018년 5월 29일
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!