Asked by CVR
on 30 Sep 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

*No products are associated with this question.*

Answer by Jan Simon
on 30 Sep 2013

Edited by Jan Simon
on 30 Sep 2013

Accepted answer

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?

CVR
on 1 Oct 2013

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 Simon
on 1 Oct 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
on 1 Oct 2013

wow, great.. It works Thx

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 1 Comment

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/88643#comment_171555

I get the error message "??? Undefined function or variable 'time'." for the second version. Do you mean "t" instead?