Errors when trying to solve fourth order ODE
14 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Walter Roberson
on 4 Nov 2021
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.
More Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!