Can't figure out how to iterate a variable defined in ode45 function.
Show older comments
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
Answers (2)
Walter Roberson
on 7 Mar 2019
0 votes
Torsten
on 7 Mar 2019
0 votes
[time,s] = ode45(@(time,s)dampedspring(time,s,b),tspan,BC);
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!