Can I return the values generated in the 'if'?
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Run_Testfunction.m:
t_final = 5;
dt = 0.01;
tspan = 0:dt:t_final;
x0 = [0 0]; % Initial conditions
[t x] = ode45(@TestFunction, tspan, x0);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TestFunction.m:
function [DxDt] = TestFunction(t,x)
a = x(1);
b = x(2);
criteria = a+b;
A = eye(2);
B = [1 1;0 1];
if criteria < 10
DxDt = A*x+1;
a = DxDt(1);
b = DxDt(2);
elseif criteria >= 10
DxDt = B*x+1;
a = 10;
b = 10;
else
DxDt = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Hi. I want to use the values(a and b) in the loop to calculate the 'criteria'. This code works well. But, I dont know the values(a and b) were returned to calculate the criteria.
0 件のコメント
回答 (1 件)
Matt Fig
2012 年 9 月 26 日
0 投票
What loop? An IF statement is not a loop. Are you saying that you want to find out what x is passed to TestFunction by Run_Testfunction every time it does so?
2 件のコメント
Use this in the TestFunction, then run it:
function [DxDt] = TestFunction(t,x)
a = x(1);
b = x(2);
criteria = a+b;
A = eye(2);
B = [1 1;0 1];
try
load('vars_testfunction')
catch
S.a = [];
S.b = [];
end
S.a = [S.a a];
S.b = [S.b b];
save('vars_testfunction','S');
if criteria < 10
DxDt = A*x+1;
elseif criteria >= 10
DxDt = B*x+1;
else
DxDt = 0;
end
After you run the code, type this at the command line:
X = load('vars_testfunction')
Now look at X.S.a and X.S.b, these are the values passed into the function. Note that you should delete vars_testfunction.mat if you run the code again with anything changed, or it will simply append the new values to those from the previous run.
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!