Borrar filtros
Borrar filtros

Can't figure out how to iterate a variable defined in ode45 function.

2 visualizaciones (últimos 30 días)
Trent Barbusca
Trent Barbusca el 7 de Mzo. de 2019
Respondida: Torsten el 7 de Mzo. de 2019
I want to run a for loop where every time I increase the size of b by one and use the data for a plot. b is a constant used in the function sdot. Basically I don't know how to change the variable b outside of sdot which should be defined in sdot.
t0 = 0;tf = 10;
l0 = 0; ldot0 = 5;
tspan = [t0,tf];
BC = [l0,ldot0];
[time,s] = ode45(@dampedspring,tspan,BC);
l = s(:,1);
ldot = s(:,2);
figure(1);
plot(time,ldot);
for i = 1:length(l)
y(i) = 0;
end
for i = 1:10
b = i;
[time,s] = ode45(@dampedspring,tspan,BC,b);
l = s(:,1);
ldot = s(:,2);
figure(i);
plot(time,ldot);
end
function sdot = dampedspring(time,s,b)
m = 2;k1 = 4;k2 = 4;
sdot = [s(2);((-b*s(2))-(k1*s(1))-(k2*s(1)))/m];
end

Respuestas (2)

Walter Roberson
Walter Roberson el 7 de Mzo. de 2019

Torsten
Torsten el 7 de Mzo. de 2019
[time,s] = ode45(@(time,s)dampedspring(time,s,b),tspan,BC);

Categorías

Más información sobre Programming 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!

Translated by