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.

답변 (1개)

Matt Fig
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

Jaekwan
Jaekwan 2012년 9월 26일
편집: Jaekwan 2012년 9월 26일
Yes it is not a loop. my mistake. Anyway, I want to know what x is passed to testfunction as you mentioned. If I want to update a and b at each step, should I put the CRITERIA in if and ifelse like below?
if criteria < 10
DxDt = A*x+1;
a = DxDt(1);
b = DxDt(2);
criteria = a+b;
Matt Fig
Matt Fig 2012년 9월 26일
편집: Matt Fig 2012년 9월 26일
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.

이 질문은 마감되었습니다.

태그

질문:

2012년 9월 26일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by