I keep getting errors when trying to use the ode 45 function to integrate an acceleration equation to determine vO vs. t, transferring all constants to the function using global variables.

This is my main code:
clc
clear
f=3500;
cd=.6;
cf=3;
m0=600;
c=2;
d=0;
dN=0;
for t=0:0.1:5
vE=0;
m=m0-(c*t);
n=0;
v=0;
a=(f/m)-((cd/m)*v);
vE=vE+(a*.1);
fprintf('VEuler is %1.2f when t is %1.1f \n',vE,t)
vO=v+vE;
dN=((5+t)*((vO+vE)/2))+d;
end
fprintf('After initiating acceleration, the dragster has moved %1.2f meters\n',dN)
y0 = 0;
tspan=0:0.1:5;
[t,y]= ode45('fun',tspan,y0);
fprintf('\n t y\n');
tvalues=transpose(t);
fxvalues=transpose(fx);
With the function:
function fx=fun(t,y0)
global f cd cf m0 c;
fx=(((cf/m0-c.*t))*y0)-((cd/m0-c.*t).*y0.^2)+(f/m0-c.*t);
end
Matlab is telling me that it's returning a vector length of zero, an ode arguements error (odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options,varargin);)
and that there's an error in the ode function itself. I've explored the internet and tried several online solutions but none have worked.

 채택된 답변

madhan ravi
madhan ravi 2018년 11월 30일
편집: madhan ravi 2018년 11월 30일
clc
clear
f=3500;
cd=.6;
cf=3;
m0=600;
c=2;
d=0;
dN=0;
for t=0:0.1:5
vE=0;
m=m0-(c*t);
n=0;
v=0;
a=(f/m)-((cd/m)*v);
vE=vE+(a*.1);
fprintf('VEuler is %1.2f when t is %1.1f \n',vE,t)
vO=v+vE;
dN=((5+t)*((vO+vE)/2))+d;
end
fprintf('After initiating acceleration, the dragster has moved %1.2f meters\n',dN)
y0 = 0;
tspan=0:0.1:5;
[t,y]= ode45(@(t,y0)fun(t,y0,f, cd ,cf, m0, c),tspan,y0);
fprintf('\n t=%f y=%f\n',t,y);
tvalues=transpose(t);
fxvalues=transpose(y); % what's fx? should be y because it's the solution which is returned from ode45
function fx=fun(t,y0,f, cd ,cf, m0, c)
fx=(((cf/m0-c.*t))*y0)-((cd/m0-c.*t).*y0.^2)+(f/m0-c.*t);
end

추가 답변 (0개)

카테고리

제품

태그

아직 태그를 입력하지 않았습니다.

질문:

2018년 11월 30일

편집:

2018년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by