2nd order ODE with time dependent parameters

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 个)

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!

类别

帮助中心File Exchange 中查找有关 상미분 방정식 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!