Redefine variable in function file in script loop

조회 수: 3 (최근 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 CenterFile Exchange에서 Specifying 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