finding the value at a specific time from second-order ODE

41 visualizaciones (últimos 30 días)
Tien Yin
Tien Yin el 2 de Dic. de 2024 a las 17:08
Comentada: Tien Yin el 4 de Dic. de 2024 a las 2:20
I'm new to MATLAB
I've been working on solving ODE and I now have learnt two ways to plot the 2-order ODE
  1. using ode45
  2. using conv() fucntion
Here's the problem : I want to know the value at a specific time for example f(2.5) when t=2.5
it can be displayed in any way like output in the command window or on the figure
please teach a way to output the value
I'll attach the files I've been working on
if you think the codes are tltr you could just give suggestions though
[time,y] = ode45('ode45_function' , [0 10] , [0,0]) ;
figure ;
subplot(212);plot(time,y(:,1));xlabel('t');ylabel('y(t)');
m = 5;
k = 18;
c = 1.2;
f0 = 100;
wn = sqrt(k/m);
cc = 2*m*wn;
Dr = c/cc;
wd = wn*sqrt(1-Dr^2);
dt = 0.001; % assumed dt
time = 0:dt:10;
f = zeros(1, length(time));
f(0001:3000) = (f0/3) * (0:dt:3-dt); % Linear increase from 0 to 3 seconds
f(3001:5000) = f0; % Constant from 3 to 5 seconds
% f remains 0 after 5 seconds
g = (1/(m*wd))*exp(-Dr*wn*time).*sin(wd*time);
x = conv(f, g)*dt;
x = x(1:length(time)); % Ensure x is the same length as time
% x is the solution function for the 2-order ODE 
figure;
subplot(221); plot(time, f, 'r'); xlabel('t(s)'); ylabel('f(t)');
subplot(222); plot(time, g); xlabel('t(s)'); ylabel('g(t)');
subplot(223); plot(time, x); xlabel('t(s)'); ylabel('位移(m)');
% this is the function which I want to extract the specific value from
% it for ex: f(3), f(2.5) ...
function ydot=ode45_function(t,y)
m=5;
k=18;
c=1.2;
if t<3
f= 100*t/3;
elseif t<5
f=100;
else
f=0;
end
ydot(1)=y(2);
ydot(2)=(1/m)*(f-c*y(2)-k*y(1));
ydot=ydot';
end

Respuesta aceptada

Steven Lord
Steven Lord el 2 de Dic. de 2024 a las 18:44
You could call ode45 with just one output argument and then call deval, or you could call ode45 with the desired time as one element of the timespan input argument, or (if you're using a sufficiently recent release of MATLAB) you could use the ode object instead of just ode45.
I also recommend you pass a function handle into ode45 rather than passing the name of the function.
One output with deval
sol = ode45(@ode45_function , [0 10] , [0,0]) ;
solutionAtTime = deval(sol, 2.5)
solutionAtTime = 2×1
5.2273 1.9079
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Timespan input contains the desired time
[t, y] = ode45(@ode45_function , [0 2.5 10] , [0,0]);
solutionAtTime2 = y(2, :).'
solutionAtTime2 = 2×1
5.2273 1.9079
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ODE object
odeobj = ode;
odeobj.ODEFcn = @ode45_function;
odeobj.InitialValue = [0 0];
results = solve(odeobj, 2.5); % Just solve at t = 2.5
solutionAtTime3 = results.Solution
solutionAtTime3 = 2×1
5.2267 1.9092
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  6 comentarios
Steven Lord
Steven Lord el 3 de Dic. de 2024 a las 15:53
Yes, if the first component of y represents position then the second component represents velocity.
Tien Yin
Tien Yin el 4 de Dic. de 2024 a las 2:20
I get it , thanks for answering

Iniciar sesión para comentar.

Más respuestas (1)

Torsten
Torsten el 2 de Dic. de 2024 a las 18:04
Movida: Torsten el 2 de Dic. de 2024 a las 18:04
The easiest (maybe slightly inaccurate) way is to use interpolation:
f_conv = interp1(time,x,2.5)
f_ode45 = interp1(time,y(:,1),2.5)
  3 comentarios
Sam Chak
Sam Chak el 3 de Dic. de 2024 a las 11:12
@Tien Yin, Also try learning to "vote" other helpful answers or solutions.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by