Same Function but Different Result (Basic Function)

조회 수: 1 (최근 30일)
CVR
CVR 2013년 9월 30일
댓글: CVR 2013년 10월 1일
Hi all, can anybody help me ? I have 2 same functions here, but when I evaluate them, I got a slightly different result. Perhaps someone could help me to check it ? Thank you
The first version is
t = [4:1:14];
A0 = 23
rho = 1.2
Ton = 1.2
Toff = 6
r = 0.6
del_a = 3.8
del_m = 0.1
p = t>=Ton;
q = t <Toff;
s = t>=Toff;
A = (A0*exp(rho.*(t - Ton)).*p.*q) + (A0*exp(rho*(Toff - Ton))*exp(-(r+del_a).*(t - Toff))).*s;
M = ((r/(r+del_a - del_m))*(A0*exp(rho*(Toff - Ton))*exp(-del_m.*(t-Toff)) - A)).*s;
y = A+M
plot(t,y)
and the second version is :
t = [4:1:14];
Par(1) = 23
Par(2) = 1.2
Par(3) = 1.2
Par(4) = 6
Par(5) = 0.6
Par(6) = 3.8
Par(7) = 0.1
model = @(Par,t) ( ( Par(1)*exp(Par(2).*(t - Par(3))).*(t>=Par(3)).*(t < Par(4)) )...
+ ( Par(1)*exp(Par(4) - Par(3))*exp(-(Par(5)+Par(6)).*(t - Par(4))).*(t >= Par(:,4)))...
+ ( (Par(5)/(Par(5) + Par(6) - Par(7))) *( (Par(1)*exp(Par(2)*(Par(4) - Par(3))))* ...
exp(-Par(7).*(t - Par(4))) - (( Par(1)*exp(Par(2).*(t - Par(3))).*(t>=Par(3)).*(t < Par(4)) )...
+ ( Par(1)*exp(Par(4) - Par(3))*exp(-(Par(5)+Par(6)).*(t - Par(4))).*(t >= Par(4)))...
) ) ).*(t>=Par(4)) ...
)
y = model(Par,time);
plot(t,y)
the problem is I have a slightly different result, but both of them are a same function. Can anybody help ? What I want to do is to convert my function in the first version into a function handle like in the second version
Thanks very much
  댓글 수: 1
Jan
Jan 2013년 9월 30일
편집: Jan 2013년 9월 30일
I get the error message "??? Undefined function or variable 'time'." for the second version. Do you mean "t" instead?

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2013년 9월 30일
편집: Jan 2013년 9월 30일
If I replace "time" by "t", I get this:
y1 = A+M;
y2 = model(Par, t);
y1 - y2
% >> 0, 0, 3875.769e, 47.58413, 0.5842066, 7.172503e-3, 8.805925e-5, 1.081133e-6, 1.327351e-8, 1.629701e-10, 1.989520e-12
This seems to show, that the functions are different.
The 2nd version is very hard to read. I'd really avoid such ugly code, because, as you already see, it is nearly impossible to debug it. Do you have any good reasons to prefer this kind of code?
  댓글 수: 3
Jan
Jan 2013년 10월 1일
You can simply write the code into a function and create a function handle either by setting "@" before the name of the function, or by using str2func.
CVR
CVR 2013년 10월 1일
wow, great.. It works Thx

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by