When I try to run the script, an error appears saying "not enough input arguments" and an error in my function, specifically on the T = T0... line and I just can't understand why. Here's my code
function dxdt = funcionVel(t,x)
M = 1200;
r = 0.25;
c = 20;
T0 = 1000;
t0 = 10;
T = T0*(1 - exp(-t/t0));
dxdt(1) = x(2);
dxdt(2) = T/(M*r) - c*x(2);
dxdt = dxdt';
end
%main code
clear variables
clc
t = (0:0.1:10);
condInicio = [0;0;0];
[T,Y] = ode45(@funcionVel,t,condInicio);
x1 = Y(:,1);
plot(t,x1)

 Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Sept. de 2020

0 votos

If you are using a single file then the script has to be before the function. Also, you had too many initial conditions.
t = (0:0.1:10);
condInicio = [0;0];
[T,Y] = ode45(@funcionVel,t,condInicio);
x1 = Y(:,1);
plot(t,x1)
function dxdt = funcionVel(t,x)
M = 1200;
r = 0.25;
c = 20;
T0 = 1000;
t0 = 10;
T = T0*(1 - exp(-t/t0));
dxdt(1) = x(2);
dxdt(2) = T/(M*r) - c*x(2);
dxdt = dxdt';
end

3 comentarios

Luis Montenegro
Luis Montenegro el 4 de Sept. de 2020
Thanks! It worked. Also, if it is not too much to ask, how can I plot x1 for multiple values of M on the same plot?
Walter Roberson
Walter Roberson el 4 de Sept. de 2020
t = (0:0.1:10);
condInicio = [0;0];
MVals = [1200 1800 2400];
for M = Mvals
[T,Y] = ode45(@(t,x) funcionVel(t,x,M), t, condInicio);
x1 = Y(:,1);
plot(t, x1, 'DisplayName', sprintf('M = %g', M));
hold on
end
legend show
function dxdt = funcionVel(t, x, M)
r = 0.25;
c = 20;
T0 = 1000;
t0 = 10;
T = T0*(1 - exp(-t/t0));
dxdt(1) = x(2);
dxdt(2) = T/(M*r) - c*x(2);
dxdt = dxdt';
end
Luis Montenegro
Luis Montenegro el 4 de Sept. de 2020
You really are the mvp!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Sept. de 2020

Comentada:

el 4 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by