HOW TO WRITE THE FUNCTION FOR ODE45

Hi. I am still new in Matlab and i don't know how to write this function for this equation. I know i must use ode45 but i don't know how to write that function coding(for eqn 21 and 22)(I attached with the journal that i referred):
I try to write it but it is have some error, this is how i write:
function dvdt=velocityspinsolver(times,velocities)
dvdt=-g-(Kv/m)*sqrt(v^2+v^2)-Dwv/m;
dvdt=(-Kv/m)*sqrt(v^2+v^2)+Kwv/m; %equation (21)
g=32.174;
K=0.00832;
m=0.59375;
D=0.001452;
w=51;
And this is the error:
Unrecognized function or variable 'g'.
Error in velocityspinsolver (line 2)
dvdt=-g-(Kv/m)*sqrt(v^2+v^2)-Dwv/m;
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);
Error in dua (line 44)
[time,velocities] = ode45('velocityspinsolver',0:.005:5, V0);

 Accepted Answer

ode45(@velocityspinsolver, [0,10],[1,1])
function dvdt=velocityspinsolver(t,y)
y = zeros(2,1);
vx = y(1);
vy = y(2);
g=32.174;
K=0.00832;
m=0.59375;
D=0.001452;
w=51;
dvdt = [-g-(K*vx/m)*sqrt(vx^2+vy^2)-D*w*vy/m;
(-K*vy/m)*sqrt(vx^2+vy^2)+K*w*vx/m]; %equation (21)
end

7 Comments

hi can i know this function just write in the same file with other or do in the other file like i do before? because for the other i need to 'called' the function of 'velocityspinsolver' like this:
for theta=0:(pi/90):(pi/2)
for speed=150:-5:0
V0=[speed*cos(theta) speed*sin(theta)];
[time,velocities] = ode45('velocityspinsolver',0:.005:5, V0);
X = zeros (length(time),2);
X(1,2)=height;
for i = 1:(length(time)-1)
X(i+1,1)=(X(i,1)+(time(i+1)-time(i))*velocities(i,1));
X(i+1,2)=(X(i,2)+(time(i+1)-time(i))*velocities(i,2));
end
Just copy paste my code in a file called Test.m and simply run it.
i got this error when i copy your script.I dont why or i need to change like this velocityspinsolver(times,velocity)?:
Error using nargin
Error: File: velocityspinsolver.m Line: 2 Column: 16
Local function name must be different from the script name.
Error in odearguments (line 60)
if (nargin(ode) == 2)
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in dua (line 44)
[time,velocities] = ode45('velocityspinsolver',0:.005:5, V0);
When i copy your script i got this result
But i need to run this function to relate in this script below because i want to know thw trajectory for this projectile.Can u help me
How’s this related to the question you originally asked?? First you asked about ode45() and now a loop?
Sorry sir because i though the function for 'velocityspinsolver' must be call in this loop. Because i need to get the polynomial graph that refer to the projectile motion. When i run and added with your script in to this loop i also get the linear graph. Soory for my mistaken. Sorry sir because i'm still learning to understand this. Here the loop that have the 'velocityspinsolver':
There's another loop from that journal.
%Initial loop, moves large incriments
q=0;
for theta=0:(pi/90):(pi/2)
for speed=150:-5:0
V0=[speed*cos(theta) speed*sin(theta)];
[time,velocities] = ode45('velocityspinsolver',0:.005:5, V0);
X = zeros (length(time),2);
X(1,2)=height;
for i = 1:(length(time)-1)
X(i+1,1)=(X(i,1)+(time(i+1)-time(i))*velocities(i,1));
X(i+1,2)=(X(i,2)+(time(i+1)-time(i))*velocities(i,2));
end
findnet=(X(:,1)<dtonet);
if sum(findnet)>length(time)
if X(sum(findnet)+1,2)> netheight
b=sum(X(:,2)>0);
if (X(b,1)>dtonet) && (X(b,1)<(dtonet+dfromnet))
q=1;
end
end
end
if q==1
break
end
end
if q==1
break
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!