Getting an error using theta

조회 수: 7 (최근 30일)
allison
allison 2023년 4월 21일
댓글: Image Analyst 2023년 4월 21일
running this code:
function [tvals, wvals] = RK4SYS(rhsf,init_t, init_y, stop_t, Nsteps)
h=(stop_t-init_t)/Nsteps;
tvals(1)=init_t;
wvals(:,1)=init_y;
for index=1:Nsteps
tvals(index+1)=tvals(index)+h;
k1=h*rhsf(tvals(index),wvals(:,index));
k2=h*rhsf(tvals(index)+h/2,wvals(:,index)+k1/2);
k3=h*rhsf(tvals(index)+h/2,wvals(:,index)+k2/2);
k4=h*rhsf(tvals(index)+h,wvals(:,index)+k3);
wvals(:,index+1)=wvals(:,index)+(k1/6+k2/3+k3/3+k4/6);
end
end
trying to do:
Rhsprob=@ (t,U)[U(2),-32.17/2*sin(theta )]
initU1=[pi/6 ; 0]
tv, wv] = RK4SYS(Rhsprob,0, initU1, 2, 5);
but i get an error when doing this line says theta requires something and error in line 15 of my code
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 4월 21일
Where do you define a value for theta?

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2023년 4월 21일
Rhsprob=@ (t,U)[U(2),-32.17/2*sin(theta )]
Rhsprob = function_handle with value:
@(t,U)[U(2),-32.17/2*sin(theta)]
Nowhere in the code you posted before this line do you define a variable named theta. So if I were to try to call this anonymous function:
Rhsprob(0, [1; 2])
Unrecognized function or variable 'theta'.

Error in solution>@(t,U)[U(2),-32.17/2*sin(theta)] (line 1)
Rhsprob=@ (t,U)[U(2),-32.17/2*sin(theta )]
MATLAB doesn't know what theta means in this context and so correctly throws an error alerting you to that fact. I don't know what theta means in this context either so I can't tell you how to correct the code.
  댓글 수: 3
Walter Roberson
Walter Roberson 2023년 4월 21일
Yes, but what is theta ?
If you are trying to create an ODE in terms of θ and then the θ to be used in the function handle would be one of the state variables, either U(1) or U(2)
Image Analyst
Image Analyst 2023년 4월 21일
Looks like @allison posted a separate question with more details.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Function Handles에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by