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

3 views (last 30 days)
Maria Cuellar on 15 Feb 2020
Edited: James Tursa on 15 Feb 2020
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

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