Errors when trying to solve fourth order ODE

조회 수: 17 (최근 30일)
Liam Hoyle
Liam Hoyle 2021년 11월 4일
댓글: Liam Hoyle 2021년 11월 4일
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