I want to use for loop while solving ode but my code is not running
Show older comments
%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 Comment
Walter Roberson
on 27 Jan 2020
We recommend against using global
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!