0

I use lsqcurvefit to fit my function. My function is:

C_PET (t)=(k_1/(α_2+α_1 ) [(k_3+k_4-α_1 )  e^((-α_1   t) )+(α_2-k_3-k_4 ) e^((-α_2   t) ) ]* C_P (t))+vC_b (t)

I went to find the solution, meaning the best fit for my K parametres. The problem is that the solution that my code give is the initial point. this is the code (vb is constant , cp,ydata,t are a vector

    k0 = 1*ones(1,4);
    k0(4) = 0;
    k0 = [0.8,0.1,0.5,0.07]
    a1=[k0(2)+k0(3)+k0(4)+sqrt(((k0(2)+k0(3)+k0(4)).^2) -4*k0(2)*k0(4))]/2;
    a2=[k0(2)+k0(3)+k0(4)-sqrt(((k0(2)+k0(3)+k0(4)).^2) -4*k0(2)*k0(4))]/2;
    l1=(k0(1)/(a2+a1))*(k0(3)+k0(4)-a1) l2=(a2-k0(3)-k0(4))
    l2=(a2-k0(3)-k0(4))
     y=conv((l1*exp(-a1*t)+l2*exp(-a2*t)),cp);

    y=(y(1:numel(t)));
    CPET=@(k,t) y+(vb*cp);
    [xfitted,errorfitted] = lsqcurvefit(CPET,k0,t,ydata)

%

So please can you hep me.

hajer
  • 11
  • 6

1 Answers1

1

Your objective function CPET is a constant:

CPET=@(k,t) y+(vb*cp);

You have declared it as a function of k and t but neither y, vb nor cp change when k or t change. That's why your solver isn't changing the answer. No matter what values of k or t lsqcurvefit feeds to CPET, the answer is always the same, which is wrong.

Your objective function is very long, so lets consider a much simpler one, say fitting a quadratic model that has another function of t as another term (i.e. in the same way that your C_P works) something like:

O = k1 + k2*t + k3*t2 + CP(t)

To write this objective function into the form expected by lsqcurvefit do:

O = @(k,t) k(1) + k(2).*t + k(3).*t.^2 + CP(t)

Now CP above can be a vector, CP, if and only if t will always be a natural number. It would make far more sense however to create a function called CP that takes t in as an input. For example maybe all CP does is take the sine of t then

CP = @(t)sin(t);  %// This needs to be declared before O

You need to write your CPET similary as a function of k and t and it needs to use both k and t in it's definition.

Dan
  • 45,079
  • 17
  • 88
  • 157
  • thank you for your answer but i didn't understend what you mean, my function CPET is a vector not a constant and y change in the equation. My objectif funtion is writen in the formule of CPET(t) – hajer Feb 19 '16 at 12:35
  • I was saying that if your `CPET` function was a simpler function (in this case a simple quadratic), then that is how you would code it. It needs to be a function of `k` and `t` so you need to rewrite your `C_PET (t)=(k_1/(α_2+α_1 ) [(k_3+k_4-α_1 ) e^((-α_1 ....` function in the same MATLAB form as I wrote my `O` function in the answer above. – Dan Feb 19 '16 at 12:39
  • your `CPET(t)` is not MATLAB syntax. I don't see how your `CPET=@(k,t) y+(vb*cp);` relates to it. Further, `lsqcurvefit` expects a function of the form [`fun(x,xdata)`](http://www.mathworks.com/help/optim/ug/lsqcurvefit.html#inputarg_fun). In your case, `x` is `k` which is a vector of parameters you want to change and `xdata` is `t`, a vector of points. At each iteration, `lsqcurvefit` passes values into `fun` and evalutates it. If the value of `fun` does not depend on `x` and `xdata` then nothing will change (e.g. `fun=@(x,xdata) 10`) which is essentially what you are doing currently – Dan Feb 19 '16 at 12:44
  • thank you a gain ,My function depend on k,t,α,cp and cb. so for that to easily coded ,i sement the function in step,when i wrote all the function in the same line ,it give me an error of convulation (Matrix dimensions must agree. becaue size of y is 53 and size of cp is 27) – hajer Feb 19 '16 at 12:50
  • please ,my function CPET depend on other paramer than k and t but the matlab function i have to use juste k and t because CPET(t)= mynfuction;can you please give me my faut exactly where. – hajer Feb 19 '16 at 13:09
  • I already did, your fault is `CPET=@(k,t) y+(vb*cp);`. I'm not going to write this code for you. You need to reread the documentation links I've provided. At present, the way you have coded `CPET` it is a constant meaning it depends on NO OTHER VARIABLE. – Dan Feb 19 '16 at 13:13
  • ok thank you ,i m really sorry ,of course i will try to fixe my code but where is this documenattions links?please and sorry again – hajer Feb 19 '16 at 13:18
  • http://www.mathworks.com/help/optim/ug/lsqcurvefit.html?refresh=true#inputarg_fun – Dan Feb 19 '16 at 13:19
  • hello again ,i tried to fixed my code and i change the line which i defined my function but still the same result ,it give me the intial point `CPET=@(k,t) numel((conv ((k(1)/(a2+a1)*((k(3)+k(4)-a1)* exp(-a1*t)+(a2-k(3)-k(4))*exp(-a2*t))),cp)))+vb*cp; ` – hajer Feb 19 '16 at 13:53
  • Why are you using `numel`? – Dan Feb 19 '16 at 14:20
  • i use numel to have the same size fot the convolution and vb*cp.(to can do the additiion ) – hajer Feb 19 '16 at 14:31
  • No, `numel` is returning a scalar. It just counts the number of elements returned by the convolution. Which by the way will also be independent of both`k` and `t` and so your function is still a constant. One way to test your function would be to manually call it on value you know the answer to e.g. `CPET([1,2,3,4], (1:10)')` and then try again with a different `k` like `CPET([5,6,7,8], (1:10)')`. You should get 20 different numbers. I reckon you'll get the same number 20 times at the moment – Dan Feb 19 '16 at 14:56
  • so you mean that all my function is wrong or just the use of numel influence the resulat ,(because if i elminate the numel ,the addition not work :the two sizes are not equal.) and par consequnce i can't have a CPET – hajer Feb 19 '16 at 15:25
  • @CPET probably your whole function is wrong, but `numel` certainly has no place in it at all – Dan Feb 22 '16 at 06:48