I want to use for loop while solving ode but my code is not running
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
%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
1 commentaire
Réponse acceptée
Walter Roberson
le 27 Jan 2020
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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!