Fitting equation in matlab

I have an equation of the form y= a0+a1log(x)+ a2log(1/x)
I want to use polyfit but I don't know how to fix the degree of the polynomial in this case. Can anyone help me please?

 Réponse acceptée

Matt J
Matt J le 16 Août 2013
Modifié(e) : Matt J le 16 Août 2013

1 vote

Since log(1/x)=-log(x) your equation model has redundant terms. It is equivalent to
y = a0+(a1-a2)*log(x)
= A+B*log(x)
where a0 has been relabeled as A and B has replaced a1-a2.
You could fit A and B, I suppose, by doing
AB=polyfit(log(x),y,1);
A=AB(2);
B=AB(1);

18 commentaires

sisay
sisay le 16 Août 2013
yes but how can I get the values of a1 and a2 ?
Matt J
Matt J le 16 Août 2013
If you agreed with me when I told you that your equation has redundant terms, then you shouldn't be interested in a1 and a2 anymore. You should be interested only in B.
sisay
sisay le 16 Août 2013
Yeah. But I want to know the values of a1 and a2...those are the two terms I want to find.
Matt J
Matt J le 16 Août 2013
Modifié(e) : Matt J le 16 Août 2013
There is no unique solution for a1 and a2. Once you have B, any pair of a1 and a2 satisyfing
B=a1-a2
will produce the same fit. That's what happens when you try to fit an equation with redundant terms.
sisay
sisay le 16 Août 2013
Yes Matt I can understand you ...even My original question is y= a0+a1log(x/22)+a2log(22/x)...
Matt J
Matt J le 16 Août 2013
Modifié(e) : Matt J le 16 Août 2013
Yes Matt I can understand you
Good! Then your question is answered. Tada!
sisay
sisay le 16 Août 2013
yeah.. but what about the case with the nominator 22
Matt J
Matt J le 16 Août 2013
Modifié(e) : Matt J le 16 Août 2013
It doesn't change anything. After re-arranging the equation, you will have
y = a0+(a1-a2)log(x)+(a2-a1)*log(22)
= A+B*log(x)
which is the same form as before with the change of variables
A=a0+(a2-a1)*log(22)
B=a2-a1
You could also rewrite as
y=A+B*(log(x/22)
and do
AB=polyfit(log(x/22),y,1)
Image Analyst
Image Analyst le 16 Août 2013
Why not just say a2=0? Since it's only the sum that has to be a certain value (B), then just say a2=0, and a1=B and be done with it.
sisay
sisay le 16 Août 2013
tnx..a2 can't be zero because of some physical reasoning...
Matt J
Matt J le 16 Août 2013
Modifié(e) : Matt J le 21 Août 2013
If there are physical constraints on the coefficients, the fitting algorithm will need to consider those constraints. The entire solution that I've given you could be invalidated if you're holding back information like that.
sisay
sisay le 16 Août 2013
No am not holding back any information. a1 and a2 are coefficients for log(x) and log(1/x) that increases upward and the other in the opposite direction .
Walter Roberson
Walter Roberson le 16 Août 2013
If a2 cannot be 0, can it be 1?
Image Analyst
Image Analyst le 16 Août 2013
But what you're saying doesn't make sense. See my code below where I set a2 = 0 and it does the fit just fine. If you think it doesn't then say why. If you want some different value of a2, then pick one and I'll adjust the a1 and it will fit just fine again.
sisay
sisay le 16 Août 2013
Modifié(e) : sisay le 16 Août 2013
Thank you for your help I understand you perfectly .To tell you briefly, in my case x refers to the depth of water and y is the concentration data . coefficients a1 and a2 should be calculated and they are assumed to relate to some physical variables that finally used ss check ups. Making one of them zero or 1 will be meaningless.
Image Analyst
Image Analyst le 16 Août 2013
sisay, you don't have enough parameters to pin down both a1 and a2. That's what everyone is trying to tell you. You know the laws of logarithms don't you? So you can see that a1 and a2 are always go together in a pair "a1-a2" - there is no way to specify each, all you can do is to specify the difference "a1-a2". Why did my code below not convince you of that?
I have no idea what "that finally used ss check ups" means. But when Matt asked you if there were some other constraints that you weren't telling us, you said no. So in that case, all answerers are in agreement that a2 can be 0 or 1 or anything you want it to be because a1 will just adjust so that the difference is what it needs to be. Why can't you follow our arguments/explanations? Did you read my comments in my code where I proved that?
Walter Roberson
Walter Roberson le 16 Août 2013
Your equation is underdetermined. There are an infinite number of (a1, a2) pairs that will work in your equation; everything along the ray starting from a2 = epsilon (epsilon being positive and arbitrarily close to zero) and up, with only the difference calculatable.
Unless, that is, you have additional information that can be used to constrain the two values.
sisay
sisay le 16 Août 2013
Thank you guys for your helping me

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 16 Août 2013

0 votes

Any fixed degree that you use will result in a polynomial that tends to be infinitely wrong as x tends to infinity.

1 commentaire

sisay
sisay le 16 Août 2013
Is there anyway of fitting this type of equation then? I find it difficult.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 16 Août 2013

0 votes

sisay, try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 24;
% Construct x.
x = linspace(.01, 40, 50);
a0 = 1;
a1 = 2;
a2 = 3;
% Create the perfect equation.
y = a0 + a1 * log(x)+ a2 * log(1./x);
subplot(3,1,1);
plot(x, y, 'b.-');
title('Noise-free signal', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Add some noise to make a noisy signal that we will fit.
yNoisy = y + 1.5 * rand(1, length(y));
subplot(3,1,2);
plot(x, yNoisy, 'b.-');
title('Noisy signal', 'FontSize', fontSize);
% Now get the fit
% y = a0 + a1 * log(x) - a2 * log(x)
% y = a0 + (a1 - a2) * log(x)
% y = (a1 - a2) * log(x) + a0
% Let newX = log(x), and (a1-a2) = coeffs(1), then
% y = coefficients(1) * newX + coefficients(2)
% so now we can use polyfit to fit a line.
newX = log(x);
coefficients = polyfit(newX, yNoisy, 1);
% Now get the fitted values
a0 = coefficients(2);
a1 = coefficients(1);
a2 = 0; % Might as well be 0 as any other value.
yFitted = a0 + a1 * log(x)+ a2 * log(1./x);
% and plot them
subplot(3,1,3);
plot(x, yNoisy, 'b.');
hold on;
plot(x, yFitted, 'r-', 'LineWidth', 3);
title('Fitted signal', 'FontSize', fontSize);
legend('Noisy data', 'Fitted signal');

Catégories

En savoir plus sur Polynomials dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by