Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Trent Barbusca
Trent Barbusca am 7 Mär. 2019
Beantwortet: Torsten am 7 Mär. 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

Antworten (2)

Walter Roberson
Walter Roberson am 7 Mär. 2019

Torsten
Torsten am 7 Mär. 2019
[time,s] = ode45(@(time,s)dampedspring(time,s,b),tspan,BC);

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by