Fitting powerlaw between two points
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
hi,
I have a set of data in x and y in excel scheet. The x and y makes a parabolic like curve, on the same graph I want to draw a power law between two points indicated by x1 and y1 (in excel sheet). As an example I have shown a picture that I did in excel but I need it in Matlab.
Thank you
0 commentaires
Réponse acceptée
John D'Errico
le 16 Mai 2023
Given two points exactly, thus:
yx = [0.047 100;
0.0382 0.1933];
y1 = yx(:,1);
x1 = yx(:,2);
You want the power law curve that passes exactly through the two points?
Effectively, you have the model curve:
x = a*y^b
With two points, and two unknown parameters, there is exactly one such curve that passes through the pair of points.
You can extract the coefficients using nothing more complicated than polyfit. That is, suppose we log the model? That gives us:
log(x) = log(a) + b*log(y)
As such, the parameters are given as:
P1 = polyfit(log(y1),log(x1),1)
a = exp(P1(2))
b = P1(1)
PLotting the two points, and the curve between them, we see:
plot(y1,x1,'bo')
hold on
fplot(@(y) a*y.^b,[0,0.047])
3 commentaires
John D'Errico
le 17 Mai 2023
Modifié(e) : John D'Errico
le 17 Mai 2023
@Faisal - please learn to use comments, to make a comment. I've moved your answer into a comment.
IF you have the relationship:
x = a*y^b
and you then log it...
log(x) = log(a) + b*log(y)
you understand what I did there, right? Now, suppose you use polyfit, as I did.
P1 = polyfit(log(y),log(x),1);
That means it will find coefficients for the model:
log(x) = c + d*log(y)
That means the first coefficient (thus P1(1)) will be d=b.
The second coefficient returned by polyfit will be the constant term. We can call it c, but in the logged model, you need to understand that log(a)=c=P1(2).
So then I had to exponentiate P1(2) to recover a.
As for fitting a parabola between TWO points, THINK ABOUT WHAT YOU ASK.
A parabola of the form you asked to find has one coefficient. One unknown. There will be no parabola of that form that fits exactly through two points.
You could use various tools to fit a parabola of the form
x = a + b*y^2
So TWO points. TWO unknown parameters. You could use the curve fitting toolbox. fit would do it there. But you have just two equations in two unknowns. Pencil and paper would suffice.
yx = [0.047 100;
0.0382 0.1933];
y1 = yx(:,1);
x1 = yx(:,2);
Given these two points, we would have the two equations:
x1(1) = a + b*y1(1)^2
x1(2) = a + b*y1(2)^2
Just subtract the two equations to find b, as:
x1(1) - x1(2) = b*(y1(1)^2 - y1(2)^2)
and then once you know b, recover a. In MATLAB code, this would be:
b = (x1(1) - x1(2))/(y1(1)^2 - y1(2)^2)
a = x1(1) - b*y1(1)^2
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!