Same Function but Different Result (Basic Function)

1 Ansicht (letzte 30 Tage)
CVR
CVR am 30 Sep. 2013
Kommentiert: CVR am 1 Okt. 2013
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 Kommentar
Jan
Jan am 30 Sep. 2013
Bearbeitet: Jan am 30 Sep. 2013
I get the error message "??? Undefined function or variable 'time'." for the second version. Do you mean "t" instead?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 30 Sep. 2013
Bearbeitet: Jan am 30 Sep. 2013
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 Kommentare
Jan
Jan am 1 Okt. 2013
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 am 1 Okt. 2013
wow, great.. It works Thx

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by