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

조회 수: 10 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by