Same Function but Different Result (Basic Function)

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 日

0 投票

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 件のコメント

CVR
CVR 2013 年 10 月 1 日
Yes you are right. I would prefer the first version now. The reason is because I need a function handle, not sure if a matlab function could be converted into a function handle
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 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

CVR
2013 年 9 月 30 日

コメント済み:

CVR
2013 年 10 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by