Numerical solution of Higher order differential equation using ODE45 and a m. archice

6 views (last 30 days)
Hello I am trying to solve this:
u=sin(t)
First, I create the function as follow:
function f=f5(t,y,u)
u=sin(t)
uu=3*diff(u)+3*u
f=zeros(5,1)
f(1)=y(2)
f(2)=y(3)
f(3)=y(4)
f(4)=y(5)
f(5)=-uu-5*y(5)-12*y(4)-16*y(3)-12*y(2)-4*y(1)
end
Then I saved it and used the command window
[t,y]=ode45(@f5,[0 5],[0;0;0;0;0]);
But there is something wrong I haven't still found it, and the error message it is:
% Unable to perform assignment because the left and right sides have a different number of elements.
%
% Error in f5 (line 9)
% f(5)=-uu-5*y(5)-12*y(4)-16*y(3)-12*y(2)-4*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);
If somebody could help me I apreciate it a lot
Thanjs

Accepted Answer

James Tursa
James Tursa on 15 Feb 2020
Edited: James Tursa on 15 Feb 2020
t is a number, not a symbol. So u = sin(t) is a number, not a symbolic function. So doing diff(u) does diff on a single value, and the result is empty, hence the downstream errors.
Work out the sin(t) derivatives differently, this is simple enough to do manually. Also, you've got a 4th order differential equation, so you should only have a 4-element state vector, not a 5-element state vector. E.g., with these definitions:
y(1) = y
y(2) = y'
y(3) = y''
y(4) = y'''
you would have the following function:
function f = f5(t,y)
u = sin(t);
u1 = cos(t);
u2 = -u;
uu = 5*u2 + 4*u1 + 2*u;
f = zeros(4,1);
f(1) = y(2)
f(2) = y(3)
f(3) = y(4)
f(4) = uu - (10*y(4) + 35*y(3) + 50*y(2) + 24*y(1));
end
  1 Comment
Sakib Javed
Sakib Javed on 30 Sep 2021
If y = Y1
y' = Y2
y'' = Y3
y''' = Y4
than
function dYdt = sample(t,Y)
u = sin(t);
u1 = cos(t);
u2 = -u;
uu = 5*u2 + 4*u1 +2*u;
Y1 = Y(1);
Y2 = Y(2);
Y3 = Y(3);
Y4 = Y(4);
dY1dt = Y2;
dY2dt = Y3;
dY3dt = Y4;
dY4dt = uu - (10*Y4 + 35*Y3 + 50*Y2 + 24*Y1);
dYdt = [dY1dt;dY2dt;dY3dt;dY4dt];
end

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!