I want to use for loop while solving ode but my code is not running
4 次查看(过去 30 天)
显示 更早的评论
%Function file
function dxdt = duffng_osc(t,x,f)
global alpha beta omega w ;
dxdt= zeros(3,1);
dxdt(1) = x(2);
dxdt(2) = f*cos(x(3))-alpha*x(2)-omega*x(1)-beta*x(1)^3;
dxdt(3) = w;
end
%Script file
clear all;
global alpha beta omega w ;
alpha = 0.5;
beta = 1;
omega = -1;
w = 1;
%-----------------------------
T_start = 0;
T_final = 3004*pi;
T_rec = 2*pi/w;
dt = pi/100;
n_rec = round(T_rec/dt);
%----------Initial Conditions-------
x0 = [0 1 0];
%------------Time Interval-----------
tspan = [T_start:dt:T_final];
%-----------Parameter range--------
range = [0.33:0.001:0.57];
for j=1:length(range)
f = range(j);
[t,X] = ode45(@(t,x) duffng_osc(t,x,f),tspan,x0);
end
采纳的回答
Walter Roberson
2020-1-27
You are overwriting the output. Try
nrange = length(range);
X = cell(nrange,1);
for j=1:nrange
f = range(j);
[t,X{j}] = ode45(@(t,x) duffng_osc(t,x,f),tspan,x0);
if j == 1
plot(t, X{j}(:,1:2));
title( sprintf('range = %f', f));
else
plot(t, X{j}(:,1:2) - X{1}(:,1:2))
title( sprintf('range %f minus range %f', f, range(1)) );
end
drawnow
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!