ode23 plotting anonymous function

I'm trying to use ode23 to solve a Euler equation, but I keep getting a linear plot when I should be getting a more logarithmic plot. Just looking for someone to help me with syntax, as I'm assuming I'm telling the code to plot the wrong values, and I'm not sure how to get the function in terms that ode23 can understand. I appreciate any help in advance!
%ode23 solution
n = 10/dt;
Fb = Wd *g *V; %Buoyancy Force
Fd= 0.5 * Cd * Wd * a * (v(i)^2); %Form drag
Fp = 3 * pi * Dv * d * v(i); %Friction drag
for j = 1:n
v1 = v(i);
v2 = v(i+1);
tspan = [0 10];
func = @(t,y) v(i)+ (((-Fb)+(-Fp)+(-Fd)+Fg)/m)*dt;
[v1,v2] = ode23s(@(v1,v2) v1+ (((-Fb)+(-Fp)+(-Fd)+Fg)/m)*dt, tspan, v1);
plot(t,y,'-o')
end

回答(1 个)

%ode23 solution
n = 10/dt;
Fb = Wd *g *V; %Buoyancy Force
Fd= 0.5 * Cd * Wd * a * (v(i)^2); %Form drag
Fp = 3 * pi * Dv * d * v(i); %Friction drag
for j = 1:n
v1 = v(i);
v2 = v(i+1);
tspan = [0 10];
func = @(t,y) v(i)+ (((-Fb)+(-Fp)+(-Fd)+Fg)/m)*dt;
[v1,v2] = ode23s(@(v1,v2) v1+ (((-Fb)+(-Fp)+(-Fd)+Fg)/m)*dt, tspan, v1);
plot(v1, v2, '-o', 'displayname', string(v(i)));
hold on
end
legend show

3 个评论

Thank you, it claned it up a bit but I'm still getting a vertical line, so my issue is with my functions I guess. I appreciate the help!
v2 = v(i+1);
You never use that value
func = @(t,y) v(i)+ (((-Fb)+(-Fp)+(-Fd)+Fg)/m)*dt;
You never use that function
[v1,v2] = ode23s(@(v1,v2) v1+ (((-Fb)+(-Fp)+(-Fd)+Fg)/m)*dt, tspan, v1);
The function you do use is a bit confusing. The first parameter that is going to be passed to the function is current time, a value in the range tspan(1) to tspan(end). Your function receives that as v1, and inside the function you add it to second term, so effectively you are declaring that time itself is part of the derivative.
But in the meanwhile you are using v1 as the initial condition for the second parameter, which is received as v2, and ignored in the function. Is v1 intended to be the initial derivative of the velocity, or is v1 intended to be the current time? Readers are going to be confused.
You use dt as part of your equation, but dt is usually intended as the change in time. But the change in time for ode23s() is variable, and is automatically taken into account.
The function you pass to ode23s() and related functions should calculate the derivative at the current time (first parameter) and boundary conditions (second parameter), and return only the derivative . You are not calculating v1 + dt * (calculated change in velocity): just return (acceleration) and it will automatically integrate to output velocity.
Thank you, that clears things up!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

版本

R2020b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by