2nd order ODE with time dependent parameters

조회 수: 4 (최근 30일)
민석 김
민석 김 2021년 11월 25일
답변: Pratyush Roy 2021년 12월 1일
I wanna solve a ODE question with matlab The question is
θ''(t) + 2θ'(t) + w* sin θ(t) = τ (t) , τ (t) = 5(1 − exp(−5t))
θ(0) = −π/6 and θ'(0) = 0
w = 10 when 0 ≤ t ≤ 5 or 8 otherwise
t = [0 10]
I wrote the code refer to examples in matlab ode45
ft = linspace(0,10,49); % w = f
for ft = 0:5
f = 10;
end
for ft = 5.2083:10 % w change of time range
f = 8;
end
gt = linspace(0,10,49);
g = 5.*(1-exp(-5.*gt)); % g = τ (t)
tspan = [0 10];
y0 = [-pi/6 0];
[t,y] = ode45(@(t,y) myode(t,y,ft,f,gt,g), tspan, y0);
plot(t,y)
function dydt = myode(t,y,ft,f,gt,g)
dydt = zeros(2,1);
dydt(1) = y(2);
dydt(2) = -2.*y(2)- f.*sin(y(1)) + g
f = interp1(ft,f,t); % Interpolate the data set (ft,f) at time t
g = interp1(gt,g,t); % Interpolate the data set (gt,g) at time t\
end
I got errors like this
numbers of left hand side and right hand side properties are different
오류 발생: untitled2>myode (33번 라인)
dydt(2) = -2.*y(2)- f.*sin(y(1)) + g
오류 발생: untitled2>@(t,y)myode(t,y,ft,f,gt,g) (20번 라인)
[t,y] = ode45(@(t,y) myode(t,y,ft,f,gt,g), tspan, y0);
오류 발생: odearguments (90번 라인)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
오류 발생: ode45 (106번 라인)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
오류 발생: untitled2 (20번 라인)
[t,y] = ode45(@(t,y) myode(t,y,ft,f,gt,g), tspan, y0);

답변 (1개)

Pratyush Roy
Pratyush Roy 2021년 12월 1일
Hi 민석 김,
While defining the myode function, one can find the interpolated function first and then compute the first and second order derivatives. The following code snippet might be helpful:
ft = linspace(0,10,49); % w = f
f = zeros(1,49);
for idx = 1:49
if ft(idx)<5.2083
f(idx) = 10;
else
f(idx) = 8;
end
end
gt = linspace(0,10,49);
g = 5.*(1-exp(-5.*gt)); % g = τ (t)
tspan = [0 10];
y0 = [-pi/6 0];
[t,y] = ode45(@(t,y) myode(t,y,ft,f,gt,g), tspan, y0);
plot(t,y)
function dydt = myode(t,y,ft,f,gt,g)
dydt = zeros(2,1);
f = interp1(ft,f,t); % Interpolate the data set (ft,f) at time t
g = interp1(gt,g,t); % Interpolate the data set (gt,g) at time t
dydt(1) = y(2);
dydt(2) = -2.*y(2)- f.*sin(y(1)) + g;
end
Hope this helps!

카테고리

Help CenterFile Exchange에서 상미분 방정식에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!