Integrate for a specific period of time
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Allison Bushman
el 12 de Sept. de 2019
Respondida: Torsten
el 13 de Sept. de 2019
Please help me. I am trying to use Euler integration to integrate
for 10 seconds with a step size of .01 seconds. Plot x versus time.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/238119/image.png)
x(0) = 1
t=0:.01:10;
x0=1;
xdot=-2*(x^3)+sin(0.5*t)*x;
for t=0:0.01:10
x=integrate(xdot,t,x0);
end
plot(t,x)
2 comentarios
Walter Roberson
el 13 de Sept. de 2019
However you do not have a differential equation, so it is not obvious to me what Euler integration would have to do with the situation.
Respuesta aceptada
Torsten
el 13 de Sept. de 2019
t=0:.01:10;
x = zeros(numel(t));
x(1) = 1;
fun_xdot = @(t,x) -2*(x^3) + sin(0.5*t)*x;
for i = 1:numel(t)-1
x(i+1) = x(i) + (t(i+1)-t(i))*fun_xdot(t(i),x(i));
end
plot(t,x)
0 comentarios
Más respuestas (1)
Robert U
el 13 de Sept. de 2019
Editada: Robert U
el 13 de Sept. de 2019
Hi Allison,
you can use one of Matlab's integrated ODE solvers to solve your differential equation. The code below makes use of ode45.
t=0:.01:10; % explicit time vector
x0=1; % boundary condition
% define function containing my ODE
myODE = @(t,x) -2 .* x^3 + sin( 0.5 .* t) .* x;
% solve ODE with ode45
[tsol,xsol] = ode45(myODE,t,x0);
% plot result as explicit solution points
plot(tsol,xsol,'.')
Kind regards,
Robert
0 comentarios
Ver también
Categorías
Más información sobre Ordinary Differential Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!