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

2 ビュー (過去 30 日間)
Trent Barbusca
Trent Barbusca 2019 年 3 月 7 日
回答済み: Torsten 2019 年 3 月 7 日
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

回答 (2 件)

Walter Roberson
Walter Roberson 2019 年 3 月 7 日

Torsten
Torsten 2019 年 3 月 7 日
[time,s] = ode45(@(time,s)dampedspring(time,s,b),tspan,BC);

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by