Errors when trying to solve fourth order ODE

1 次查看(过去 30 天)
This is for a homewok and I've found myself stuck. I wrote the ODE as a function handle, seems right to me but I'm very new to this language...
My code:
close all; clear all; clc
% constants
A = 2; %coefficient
%initial conditions
y0 = [1 0]; %initial conditions of y and dy/dt
t0 =0;
tf = 20;
tspan = [t0, tf];
%function handle for system of ODEs
odefun = @(t,y) [y(2);... %y1'
y(3);... %y2'
y(4);... %y3'
-(A*y(3)+y(1))]; %y4'
%...Solve using ode45
[t, y] = ode45(odefun, tspan, y0); %ode45 function call
The actual ODE I need to solve is y'''' + 2y'' +y =0 , I think it's an issue with my function handle.
Here's the errors:
Index exceeds the number of array elements. Index must not exceed 2.
Error in Problem_1_Lab_2>@(t,y)[y(2);y(3);y(4);-(A*y(3)+y(1))] (line 19)
y(3);... %y2'
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 106)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in Problem_1_Lab_2 (line 25)
[t, y] = ode45(odefun, tspan, y0); %ode45 function call
Apologies if the error messages make my issue blatanly obvious. My train of thought is that if y(1) = x, then y(3) = x'' and y(4)' = x'''', so I solved the ODE for x'''' and have that result as the final ODE in the handle.

采纳的回答

Walter Roberson
Walter Roberson 2021-11-4
y0 = [1 0]; %initial conditions of y and dy/dt
Two initial conditions
%function handle for system of ODEs
odefun = @(t,y) [y(2);... %y1'
y(3);... %y2'
y(4);... %y3'
-(A*y(3)+y(1))]; %y4'
but you use up to y(4) as inputs and you output 4 items. You need 4 input conditions, one for y1, y2, y3, y4.
  1 个评论
Liam Hoyle
Liam Hoyle 2021-11-4
Yes! I just realized this through trial and error. The input conditions for y3 and y4 were both zero, so I set them to that. Now my code spits out no errors and I can graph! Thank you very much for your answer this was my first time on the forum and I very much appreciate any and all assistance in my work (:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by