# Curve Fitting problem

1 view (last 30 days)

Show older comments

So I am trying create a script that create a fit line to a set of data. The trend in the data is most certainly the sum of a sin wave plus a series of exponential functions (two exponentials for a quite good approximation). I have been working on a script to get matlab to calculate the exact function. The script does a fine job at mathcing sin wave however it will not include the exponential part of the function, no matter what guess I put in for the exponential constants matlab returns the same values. Any suggestions on what is wrong or perhaps a better way to make this better? Below I have included two scripts I have written to try and accomplish this, neither worked, also I have tried simply using the cftool and have run into the same problem.

Script 1

x=IGORCurveFit(1:771,1);

y=IGORCurveFit(1:771,6);

plot(x, y, 'ro')

title('BtmPorePressure vs. Time');

xlabel('Time (s)');

ylabel('Pressure (MPa)');

a_guess=.2;

b_guess=.025;

c_guess=.785;

d_guess=5;

e_guess=10;

f_guess=50;

g_guess=100;

h_guess=12;

C0=[a_guess b_guess c_guess d_guess e_guess f_guess g_guess h_guess];

func = @(B,x)(B(1)*sin(B(2)*x+B(3))+B(4)*exp(-(B(5)^2)*x)-B(6)*exp(-((B(7)^2)*x))+B(8));

C = nlinfit(x,y,func,C0);

a_calc = C(1);

b_calc = C(2);

c_calc = C(3);

d_calc = C(4);

e_calc = C(5);

f_calc = C(6);

g_calc = C(7);

h_calc = C(8);

disp('Compare the solutions to the actual values')

fprintf('''a'' actual =%g, a_guess = %.4f\n', a_calc,a_guess);

fprintf('''b'' actual =%g, b_guess = %.4f\n', b_calc,b_guess);

fprintf('''c'' actual =%g, c_guess = %.4f\n', c_calc,c_guess);

fprintf('''d'' actual =%g, d_guess = %.4f\n', d_calc,d_guess);

fprintf('''e'' actual =%g, e_guess = %.4f\n', e_calc,e_guess);

fprintf('''f'' actual =%g, f_guess = %.4f\n', f_calc,f_guess);

fprintf('''g'' actual =%g, g_guess = %.4f\n', g_calc,g_guess);

fprintf('''h'' actual =%g, h_guess = %.4f\n', h_calc,h_guess);

y_new = func(C,x);

hold on

plot (x,y_new)

legend('Raw Data', 'Fitted Curve')

Attempt 2

x=IGORCurveFit(1:771,1);

y=IGORCurveFit(1:771,6);

plot(x, y, 'ro')

title('BtmPorePressure vs. Time');

xlabel('Time (s)');

ylabel('Pressure (MPa)');

%function F = myfun(a,data)

F= @(a,x) (a(1)*sin(a(2)*x+a(3))+a(4)*exp(-(a(5)^2)*x)-a(6)*exp(-((a(7)^2)*x))+a(8));

data = [x;y];

a0 = [.2, .025, .785, 5, 10, 5, 10, 12];

C = lsqcurvefit(F,a0,x,y);

[a,resnorm] = lsqcurvefit(F,a0,x,y)

y_new = F(C,x);

hold on

plot (x,y_new)

legend('Raw Data', 'Fitted Curve')

##### 3 Comments

Image Analyst
on 29 Mar 2012

### Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!