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.

Answers (1)

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 Comments

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;
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.

This question is closed.

Tags

Asked:

on 26 Sep 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!