0

I have a data-set which is loaded into matlab. I need to do exponential fitting for the plotted curve without using the curve fitting tool cftool.

I want to do this manually through executing a code/function that will output the values of a and b corresponding to the equation:

 y = a*exp(b*x)

Then be using those values, I will do error optimization and create the best fit for the data I have.

Any help please?

Thanks in advance.

1
  • Hi, is your curve a graph of a function, namely (x,a*exp(b*x))? In this case I would suggest standard methods, minimizing the least-square error by Levenberg-Marquardt, Gradient Descent, Gauß-Newton. If you want to save time and if it is available use the optimization toolbox's function lsqnonlin. Example: data_x = [0,1,2,3]; data_y = [0.5,1.5,4.5,13.5]; residua = @(x) x(1) * exp(data_x * x(2)) - data_y; lsqnonlin(residua,[1,1])
    – matheburg
    May 26, 2014 at 10:06

3 Answers 3

1

Try this...

f = fit(x,y,'exp1');
0

I think the typical objective in this type of assignment is to recognize that by taking the log of both sides, various methods of polynomial fit approaches can be used.

    ln(y) = ln(a) + ln( exp(x).^b ) 
    ln(y) = ln(a) + b * ln( exp(x) )

There can be difficulties with this approach when errors such as noise are involved due to the behavior of ln as it approaches zero.

0

In this exercise I have a set of data that present an exponential curve and I want to fit them exponentially and get the values of a and b. I used the following code and it worked with the data I have.

"trail.m" file: 
 %defining the data used 
 load trialforfitting.txt;
 xdata= trialforfitting(:,1); 
 ydata= trialforfitting(:,2); 

 %calling the "fitcurvedemo" function
 [estimates, model] = fitcurvedemo(xdata,ydata)
 disp(sse); 

 plot(xdata, ydata, 'o'); %Data curve

 hold on
 [sse, FittedCurve] = model(estimates);

 plot(xdata, FittedCurve, 'r')%Fitted curve
xlabel('Voltage (V)')
ylabel('Current (A)')
title('Exponential Fitting to IV curves');
legend('data', ['Fitting'])
hold off

"fitcurvedemo.m" file: 
function [estimates, model] = fitcurvedemo(xdata, ydata)
%Call fminsearch with a random starting point.
start_point = rand(1, 2);
model = @expfun;
estimates = fminsearch(model, start_point);
%"expfun" accepts curve parameters as inputs, and outputs 
%the sum of squares error [sse] expfun is a function handle;
%a value that contains a matlab object methods and the constructor
%"FMINSEARCH" only needs sse
%estimate returns the value of A and lambda
%model computes the exponential function 
    function [sse, FittedCurve] = expfun(params)
        A = params(1);
        lambda = params(2);
        %exponential function model to fit
        FittedCurve = A .* exp(lambda * xdata); 
        ErrorVector = FittedCurve - ydata;
        %output of the expfun function [sum of squares of error]
        sse = sum(ErrorVector .^ 2);

     end
 end

I have a new set of data that doesn't work with this code and give the appropriate exponential fit for the data curve plotted.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.