I’m doing homework; actually basic class. I just wrote everything on the given material, but I can’t still get the answer. Here’s codes:
xdata = { 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.55 1.6 1.65 1.7 1.75 1.8 1.85 1.9 1.95 2 };
ydata = { 5.17641 4.20461 3.31091 2.60899 2.23541 1.97771 1.88141 1.62821 1.50138 1.51075 1.4085 1.26222 1.09432 1.13202 1.12918 1.10355 1.11867 1.0974 1.08324 1.05687 1.19422 1.22984 1.34516 1.19713 1.25398 1.29885 1.33658 1.31166 1.40332 1.3955 1.37855 1.41491 1.59549 1.56027 1.63925 1.7244 1.74192 1.82049 };
fun = @(p) sum((ydata-(1/3)*(xdata.^2 + (2*(1+p)/xdata)))^2);
pguess = 0.1;
[p,fminres] = fminsearch(fun,pguess)
What is the problem with these codes? Would you help me?

 採用された回答

Stephen23
Stephen23 2019 年 4 月 29 日
編集済み: Stephen23 2019 年 4 月 29 日

1 投票

"I just wrote everything on the given material"
If that is the case then that material is wrong:
  • {} creates a cell array (similar to a list in other languages),
  • [] concatentates those numeric scalars into one numeric array:
xdata = [0.15 0.2 ...1.9 1.95 2];
^ ^ Creates a numeric array from numeric scalars!
It seems that your function is also missing a few characters:
fun = @(p) sum((ydata-(1/3)*(xdata.^2 + (2*(1+p)./xdata))).^2);
^ ^
After which i get:
p = 0.19589
fminres = 0.26543

1 件のコメント

KyungDeok Baek
KyungDeok Baek 2019 年 4 月 29 日
Thank you for your nice answer!

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

その他の回答 (1 件)

KSSV
KSSV 2019 年 4 月 29 日
編集済み: KSSV 2019 年 4 月 29 日

1 投票

xdata = [0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1 1.05 1.1 1.15 1.2 1.25 1.3 1.35 1.4 1.45 1.5 1.55 1.6 1.65 1.7 1.75 1.8 1.85 1.9 1.95 2 ];
ydata = [ 5.17641 4.20461 3.31091 2.60899 2.23541 1.97771 1.88141 1.62821 1.50138 1.51075 1.4085 1.26222 1.09432 1.13202 1.12918 1.10355 1.11867 1.0974 1.08324 1.05687 1.19422 1.22984 1.34516 1.19713 1.25398 1.29885 1.33658 1.31166 1.40332 1.3955 1.37855 1.41491 1.59549 1.56027 1.63925 1.7244 1.74192 1.82049 ];
fun = @(p) sum((ydata-(1/3).*(xdata.^2 + (2*(1+p)./xdata))).^2);
pguess = 0.1;
[p,fminres] = fminsearch(fun,pguess)
{} replace by []. {} is a cell. [] is a matrix.

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by