MATLAB Answers

ODE45; Length of initial conditions vector not accepted

2 views (last 30 days)
Good afternoon,
I was trying to solve this code by ode45 solver.
A_0 = (4.654*W*cos(t)*k)/(rho*S) - W*sin(t)*R/m;
A_1 = P*R/m;
A_2 = (2.327*m*k)/(rho*S*R) - (0.525*rho*S*c_Do*R)/m;
A_3 = (k*2.327*R*W^2*(cos(t)).^2)/(rho*S*m);
tspan = [-pi/6 pi/3];
v0 = 188.1062;
[t,v] = ode45(@(t,v) A_0*v^(-1) + A_1*v^(-2) - A_2*v - A_3*v^(-3),tspan,v0);
The thing is that being all the variables mentioned constants, it returns me the following error:
"Error using odearguments (line 95)
@(T,V)A_0*V^(-1)+A_1*V^(-2)-A_2*V-A_3*V^(-3) returns a vector of length 41, but the length of initial conditions vector is 1. The vector
returned by @(T,V)A_0*V^(-1)+A_1*V^(-2)-A_2*V-A_3*V^(-3) and the initial conditions vector must have the same number of elements."
I can't understand it at all because even using v0 as a vector of length 41 (with the same initial value) it returns me this other error:
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);
I've been searching for other questions on the forum related with the same error but any fits at all with my problem.
All the help is apreciated, thank you very much.

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 29 Nov 2020
See if this does what you want:
function ddx = odefcn(t,v,W,k,rho,S,R,m,P,c_Do)
A_0 = (4.654*W*cos(t)*k)/(rho*S) - W*sin(t)*R/m;
A_1 = P*R/m;
A_2 = (2.327*m*k)/(rho*S*R) - (0.525*rho*S*c_Do*R)/m;
A_3 = (k*2.327*R*W^2*(cos(t)).^2)/(rho*S*m);
ddx = A_0/v + A_1/v^2 - A_2*v - A_3/v^3;
end
then call it in ode45 as:
[t,v] = ode45(@(t,v) odefcn(t,v,W,k,rho,S,R,m,P,c_Do),tspan,v0);
I have no idea what the constants are, so I cannot test this, and am posting it as UNTESTED CODE.
If you create a separate function file to include the constants and other related code, you can run this in that function. Otherwise, save it as its own function file as odefcn.m (or whatever you choose to call it if you rename it), and then run it from ode45. In recent releases, you can put it at the end of a script file and run the code calling it in that script. See Function Basics (and related documentation) for details.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!