ode 45 and for loop

5 views (last 30 days)
Tuur Vanderhaegen
Tuur Vanderhaegen on 23 Nov 2020
Commented: Tuur Vanderhaegen on 23 Nov 2020
Hi,
I would like to use a for loop for a constant value i in my diff.eq. but I don't know how to implement it. Also I would like to have the matrices X and T for al the values of i.
for i : 0.1:0.1:10
[T,X] = ode45(@fx, [0 4], [0 0]) %Can I implement i here and give the matrices X and T an index?
end
function dx = fx(t,x) % Can I give i as an input here.
rho = 1.225;
C_w = 0.3;
A = 0.005;
m = 0.314;
C_r = 0.035;
R_T = 0.7;
n_0 = 378.33;
i = 1;
T_stall = 0.1868;
r = 0.030;
dx = zeros (2,1);
dx(1) = x(2);
dx(2) = (-T_stall * R_T * i^2 * ((1000 * x(2)/ pi ) - n_0/i)/(n_0 * r) - m * 9.81 * C_r - 0.5 * rho * A * C_w * x(2)^2)/m;
% the diff. eq. with i
end

Accepted Answer

Stephan
Stephan on 23 Nov 2020
k = 0;
T_sol = cell(1,100);
X_sol = cell(1,100);
for ii = 0.1:0.1:10
k = k+1;
[T,X] = ode45(@(t,x)fx(t,x,ii), [0 4], [0 0]);
T_sol{k} = T;
X_sol{k} = X;
end
% plot first and last solution
plot(T_sol{1}, X_sol{1})
hold on
plot(T_sol{100}, X_sol{100})
hold off
function dx = fx(~,x,ii) % Can I give i as an input here.
rho = 1.225;
C_w = 0.3;
A = 0.005;
m = 0.314;
C_r = 0.035;
R_T = 0.7;
n_0 = 378.33;
T_stall = 0.1868;
r = 0.030;
dx = zeros (2,1);
dx(1) = x(2);
dx(2) = (-T_stall * R_T * ii.^2 * ((1000 * x(2)/ pi ) - n_0/ii)/(n_0 * r) - m * 9.81 * C_r - 0.5 * rho * A * C_w * x(2)^2)/m;
% the diff. eq. with i
end

More Answers (0)

Categories

Find more on Programming 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!