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?

 Réponse acceptée

Stephen23
Stephen23 le 29 Avr 2019
Modifié(e) : Stephen23 le 29 Avr 2019

1 vote

"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

Plus de réponses (1)

KSSV
KSSV le 29 Avr 2019
Modifié(e) : KSSV le 29 Avr 2019

1 vote

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.

Catégories

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

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by