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!