Solve ODE with input vector using ode45
9 views (last 30 days)
Show older comments
I'm trying to use ode45 to solve an ODEs with vector inputs, as shown:
function dAng= angDot(t,ang,w)
% Euler angle initial conditions
phi= ang(1); thet= ang(2); psi= ang(3);
% angular velocities
P= w(:,1); Q= w(:,2); R= w(:,3);
% C= [1 sin(phi)*tan(thet) cos(phi)*tan(thet); 0 cos(phi) -sin(phi);
% 0 sin(phi)/cos(thet) cos(phi)/cos(thet)];
% Euler angle rates
dAng= [];
dAng(1,:)= P' + (sin(phi)*tan(thet).*Q') + (cos(phi)*tan(thet).*R');
dAng(2,:)= (cos(phi).*Q') - (sin(phi).*R');
dAng(3,:)= (sin(phi)*sec(thet).*Q') + (cos(phi)*sec(thet).*R');
And solving it with
%Initial conditions
angICa= [0 0 0];
[Ta,angA]= ode45(@(t,ang) angDot(t,ang,Wa),Tspan,angICa);
The term Wa is a 101x3 matrix of angular velocities that was previously calculated with ode45 and a different function. Inside the angDot function split it into 3 column vectors P, Q, and R. I transpose them in the dAng equations so that they agree with the dimensions of dAng. When I run ode45, I'm given the following error:
Error using odearguments (line 91)
@(T,ANG)ANGDOT(T,ANG,WA) must return a column vector.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in MEM453_HW03 (line 27)
[Ta,angA]= ode45(@(t,ang) angDot(t,ang,Wa),Tspan,angICa);
My goal is to find the angles ('angA') corresponding to each value in P, Q, and R. Both ODEs are using the same time step([0:100]), and the first ODE function used to solve for 'Wa' was set up in the same fashion. The only difference is that it didn't have a separate input, only its time and initial conditions.
0 Comments
Answers (1)
Satyanarayan
on 15 Jul 2014
Edited: Satyanarayan
on 15 Jul 2014
I do not know if you are still interested in this question. Are you trying to solve a diffeqn or is it just a function from a set of ready made values of w? I got errors similar to what you get for my problem, till I resolved them as below for my example. Hope this helps you.
% Set time
tspan = [0 10];
% I give an example of same diff eqn but different values of parameters
% Since my example is a second order diff eqn, I have two values y(1) and y(2)
% Since there are three ckts there are three values for each ckt for y(1) and y(2)
y0 = [0 0 0 0.5 0.5 0.5]; % y(1) for each R,L,C combination followed by y(2)
R= [1 0.1 0.2]';
L =[0.5 0.1 0.2 ]';
C= [0.5 1 2]';
V= [1 1 1 ]';
%solve the ode
[t,y]=ode45(@fde, tspan,y0,[],V,R,L,C);
% Plot the results
plot(t,y(:,1),t,y(:,2),t,y(:,3))
title(' Solution of First Order DE in MATLAB');
xlabel('time t');
ylabel('Solution of i')
legend('y_1','y_2','y_3');
And the function is
function dydt = fde(t,y,V,R,L,C)
i=length(R);
dydt= [ y(1:i).* (-R./L) - (y(i+1:2*i).* (1./L)) + V./L;
y(1:i).* (1./C) ];
So the code works as desired. Maybe is this what you want?
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!