Got Questions? Get Answers.
Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
curve fit

Subject: curve fit

From: Shirley Zheng

Date: 7 Jan, 2011 15:20:21

Message: 1 of 3

Hello, friends:

Who can help me with the following issue??

I have a set of (x, y) data. I am going to find the coefficent (a, b) of Y = a*Log(x) +b. I did the following steps:

1. convert x data to Log data.
2. use polyfit to find a, b of Y = a*Log(x)
3. I use polyfit to find a and b.
 
By using poly(xfit, yift), I can only draw curve of y and log(x), which is a straight line. how to draw the fitting curve of x and y but using the coefficient a and b? Please try to run the following code:

Y = [9,1,2,3,4,5,6,7,8];
X = [0.65,14.84,10.64,7.48,5.40,3.17,1.69,1.45,0.92];

X_Log =[];
for c = 1:length(X),
    X_Log = cat(2,X_Log,log(X(c)));
end

[X_Log_Sorted index] = sort(X_Log,'ascend');
Y_Sorted = Y(index);
X_Sorted = X(index);
             
z = (X_Log_Sorted - mean(X_Log_Sorted))/std(X_Log_Sorted); %for log data
zfit = linspace(z(1), z(end), 9);
[pz] = polyfit(z, Y_Sorted,1) %find the coefficient of y = a*LN(x)
[yfit] = polyval(pz, zfit)
xfit = linspace(X_Sorted(1), X_Sorted(end), 9)

f = figure;
scatter(X_Sorted,Y_Sorted,5,[1 0 1]);
xlabel('X');
ylabel('Y');
title('Calibration Curve');
hold on
plot(xfit,yfit,'--bs','LineWidth',1,...
                 'MarkerEdgeColor','k',...
                 'MarkerFaceColor','g',...
                 'MarkerSize',1);
hold off
    

Thanks a lot

Subject: curve fit

From: Miroslav Balda

Date: 7 Jan, 2011 16:19:05

Message: 2 of 3

Hi Shirley,

Try the function LMFnlsq at
     http://www.mathworks.com/matlabcentral/fileexchange
One of testing examples is just your function.

Mira

Subject: curve fit

From: Miroslav Balda

Date: 7 Jan, 2011 16:40:22

Message: 3 of 3

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <ig7edo$cdn$1@fred.mathworks.com>...
> Hi Shirley,
>
> Try the function LMFnlsq at
> http://www.mathworks.com/matlabcentral/fileexchange
> One of testing examples is just your function.
>
> Mira

Excuse me, I didn't read your question rigorously. Your problem is linear in coefficients a and b. LMFnlsq is too complicared, because it solves nonlinear problems. Here is the linear solution:

% Shirley.m
%%%%%%%%%%%%
Y = [9,1,2,3,4,5,6,7,8];
X = [0.65,14.84,10.64,7.48,5.40,3.17,1.69,1.45,0.92];
lX= log10(X);

A = [lX', ones(length(X),1)];
c = A\Y';
a = c(1);
b = c(2);

plot(lX,Y,'o', lX,lX*a+b,'-r')

Best regards

Mira

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us