Can I return the values generated in the 'if'?
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
Réponses (1)
Matt Fig
le 26 Sep 2012
0 votes
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 commentaires
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.
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!