How can I make logaritmic fitting like polyfit function

Hi;
I want to make logaritmic fitting but I don't find any logfit function like polyfit. Is there any function,script etc. that make logaritmic curve fitting in matlab
-edit- (19.10.2018 - 15:20)
I want to this type of curve fitting.

1 Comment

Could you give an example of what a multi-term model might look like?

Sign in to comment.

 Accepted Answer

Fit log(x) against y using a linear polynomial in "polyfit". What's the problem ?

6 Comments

"like polyfit" might imply multiple terms could be present, but it is not immediately obvious how the terms might be joined. Sum of logs would be a challenge; product would be easier.
Anyhow, if you polyfit(x, log(y)) then you have the issue that the least squared calculation is being applied in log space, not in the original space.
Actually, we have problem with our curve goes to below zero when we use polyfit function but we don't want to curve doesn't fall below to zero. Is there any solution to this problem other than logfit function
Try
p = polyfit(x,log(y),1);
b = p(1);
a = p(2);
yfit = exp(a+b*x);
plot(x,y,x,yfit)
or directly use a nonlinear fitting tool:
fun = @(p,x) p(2)*exp(p(1)*x);
p0 = [1, 1];
p = lsqcurvefit(fun,p0,x,y)
yfit = fun(p,x);
plot(x,y,x,yfit)
p = polyfit(x,log(y),1);
b = p(1);
a = p(2);
yfit = exp(a+b*x);
plot(x,y,x,yfit)
this code not suitable for me because I have input data and output data and I want to fit my curve according to my measured data. For example I have x-matrices and x=[1 2 3 4 5 6].
Also, I have measured output data, ydata matrices. My ydata values come from a device and ydata behave like decreasing exponential function. I want to y matrice, fit my real ydata matrice according to logfit.
Firstly I tried polyfit, but it didn't work properly for some x values. And now; I want to try decreasing exponential function for these x values.
x are your input data, y are your output data in both suggestions.
If you have matrices of input and output data, make them to vectors by using
x = x(:);
y = y(:);
Best wishes
Torsten.
I have not problem with convert matrices to vectors.
fun = @(p,x) p(2)*exp(p(1)*x);
p0 = [1, 1];
p = lsqcurvefit(fun,p0,x,y)
yfit = fun(p,x);
plot(x,y,x,yfit)
I change my code according second code you suggest, then it solves my problem .
Thanks for all thing;

Sign in to comment.

More Answers (0)

Categories

Asked:

on 19 Oct 2018

Commented:

on 22 Oct 2018

Community Treasure Hunt

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

Start Hunting!