Solve the IVP y′′′=sin(y2)+y′+cos(t), provided with an appropriate set of initial conditions of your choice, and using Matlab's ODE45 function.

4 ビュー (過去 30 日間)
Jack Allen
Jack Allen 2021 年 11 月 19 日
回答済み: Alan Stevens 2021 年 11 月 19 日
I decided to unpit the range [0,10] and the step size 0.1. Im a little lost on what exactly im doing wrong with this situation. The code I have written so far is this but its not working yet. Im also newer to matlab as well so a little context would be amazing!
clc
clear all
%IVP y''' = sin(y^2)+y'+cos(t), over [0,10] y0 = 0.1
%y1=y, y2=y', y3=y'', y1'=y'=y2, y2'=y''=y3
%y''' = y3' = sin(y1^2)+y2+cos(t)
drdt = @(t,y) [y(2);sin(y(1)^2)+y(2)+cos(y(1))];
range = [0,10];
y0 = 0.1;
[t,y]=ode45(dydt,range,y0);
after the code runs it produces this error:
Index exceeds the number of array elements (1).
Error in DB11>@(t,y)[y(2);sin(y(1)^2)+y(2)+cos(y(1))] (line 8)
dydt = @(t,y) [y(2);sin(y(1)^2)+y(2)+cos(y(1))];
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in DB11 (line 11)
[t,y]=ode45(dydt,range,y0);
Thanks so much in advance

回答 (1 件)

Alan Stevens
Alan Stevens 2021 年 11 月 19 日
Like this? (Note the IC for d^2y/dt^2 must be consistent with the values chosen for y(0) and dy(0)/dt). Change the IC's and time range to your heart's content!
%IVP y''' = sin(y^2)+y'+cos(t), over [0,10] y0 = 0.1
%y1=y, y2=y', y3=y'', y1'=y'=y2, y2'=y''=y3
%y''' = y3' = sin(y1^2)+y2+cos(t)
% I find it easier to think of with v = dy/dt and w = dv/dt = d^2y/dt^2
dydt = @(t,y) [y(2); y(3); sin(y(1)^2)+y(2)+cos(t)]; %%%%%%% cos(t) or cos(y(1))?
range = [0,1];
y0 = 0.1; % dy/dt = v
v0 = -0.2; % dv/dt = w
w0 = sin(y0^2) + v0 +cos(0); % dw/dt = sin(y^2)+v+cos(t)
IC = [y0, v0, w0]; % You need initial conditions for y1, y2 and y3
[t,Y]=ode45(dydt,range,IC);
y = Y(:,1); v = Y(:,2); w = Y(:,3);
plot(t,y),grid
xlabel('t'), ylabel('y')

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by