How do I make custom power trendline plots?

12 ビュー (過去 30 日間)
HyoJae Lee
HyoJae Lee 2022 年 7 月 10 日
コメント済み: Star Strider 2022 年 7 月 11 日
I have a question regarding to this answer.
The link is below:
I want to make a power fitting curve in the form of y=a*x^0.5+c.
However, the linked answer just provide full prediction of the fitting curve including exponent of the curve.
I want to fix the exponent as 0.5 or other value as a constant. Finally, I just want to figure out the number of a and c.
How do I revise the code in this case?
Thank you.

採用された回答

Star Strider
Star Strider 2022 年 7 月 10 日
Try something like this —
yfcn = @(b,x) b(1).*sqrt(x) + b(2);
x = 0:0.5:10;
y = rand(size(x));
[B,fval] = fminsearch(@(b) norm(y-yfcn(b,x)), rand(2,1))
B = 2×1
0.0233 0.4282
fval = 1.3888
xv = linspace(min(x), max(x));
yv = yfcn(B,xv);
figure
plot(x, y, '.')
hold on
plot(xv, yv, '-r')
hold off
grid
xlabel('X')
ylabel('Y')
title(sprintf('$y(x) = %.3f \\cdot \\sqrt{x} + %.3f$', B), 'Interpreter','latex')
NOTE — Using ‘sqrt(x)’ is computationally more efficient than ‘x.^0.5’.
.
  4 件のコメント
HyoJae Lee
HyoJae Lee 2022 年 7 月 11 日
Last question! (sry...)
Is there any way to get r square value of the trendline?
Thanks!
Star Strider
Star Strider 2022 年 7 月 11 日
That would be easiest using the fitnlm function. It will do the fit and produce many relevant statistics, including the value.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by