Redefine variable in function file in script loop

3 visualizzazioni (ultimi 30 giorni)
Amie Theall
Amie Theall il 16 Gen 2020
Risposto: Geoff Hayes il 16 Gen 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

Risposte (1)

Geoff Hayes
Geoff Hayes il 16 Gen 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

Categorie

Scopri di più su Specifying Target for Graphics Output in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by