# Redefine variable in function file in script loop

3 ビュー (過去 30 日間)
Amie Theall 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

#### 0 件のコメント

サインイン to comment.

### 回答 (1 件)

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

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by