Redefine variable in function file in script loop

1 回表示 (過去 30 日間)
Amie Theall
Amie Theall 2020 年 1 月 16 日
回答済み: Geoff Hayes 2020 年 1 月 16 日
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

回答 (1 件)

Geoff Hayes
Geoff Hayes 2020 年 1 月 16 日
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

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by