Redefine variable in function file in script loop

3 Ansichten (letzte 30 Tage)
Amie Theall
Amie Theall am 16 Jan. 2020
Beantwortet: Geoff Hayes am 16 Jan. 2020
I want to create a function file but redefine one of the variables(N) in each iteration of a loop within my script. Tips?
Function File:
function [dw] = Problem2a(t,w)
a=5.05; b=2; N=.6
dw = a*(w^N)-b*w;
end
In script:
tspan = [0 20]; w0=0.5;
n=[0.60 0.62 0.64 0.66 0.68 0.70];
for x=1:length(n)
N=n(x);
[t,w]=ode45(@Problem2a,tspan,w0);
end

Antworten (1)

Geoff Hayes
Geoff Hayes am 16 Jan. 2020
Amie - you could try nesting your Problem2a function within your main code (note that you would need to change your script to a function (the main function/file in the below is named myMainProgram)
function myMainProgram
tspan = [0 20]; w0=0.5;
n=[0.60 0.62 0.64 0.66 0.68 0.70];
function [dw] = Problem2a(t,w)
a=5.05; b=2;
dw = a*(w^N)-b*w;
end
for x=1:length(n)
N=n(x);
[t,w]=ode45(@Problem2a,tspan,w0);
end
end

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by