Why does the programm do not run the script and continues to get unexpected expression?

x = linspace (0,10);
x = 0:0.5:10;
y = 2-10*x.*exp(-.8*x);
plot (x, y); grid on;
x = 0
y = 2-10*x.*exp(-.8*x)
f = @(x) 2-10*x.*exp(-.8*x)
[xr fx] = fzero(f, 0)
[xrr fx] = fzero(f, 4)
[xmin, fmin]=fminbnd(f,1,2)
%PART 2
line12>>subplot(1,2,2) surf(xgrid,ygrid,func(xgrid,ygrid))
[xgrid, ygrid]=meshgrid(-2:0.1:0,0:0.1:3);
func= @(x,y) 2+x-y+2*x.^2+2*x.*y+y.^2
subplot(1,2,1); contour(xgrid,ygrid,func(xgrid,ygrid))
subplot(1,2,2); surf(xgrid, ygrid, func(xgrid,ygrid))
f = @(x) 2+x(1)-x(2)+2*x(1)^2+2*x(1)*x(2)+x(2)^2
[x, fval]=fminsearch(f,[-.5, 0.5])

 採用された回答

Star Strider
Star Strider 2016 年 3 月 29 日
You cannot run your assignments and commands in any order. You have to write your script so that MATLAB has functions and arguments in its workspace in the order it needs for you to state them.
With a bit of editing of the order of the statements in your code, this works:
x = linspace (0,10);
x = 0:0.5:10;
y = 2-10*x.*exp(-.8*x);
plot (x, y); grid on;
x = 0
y = 2-10*x.*exp(-.8*x)
f = @(x) 2-10*x.*exp(-.8*x)
[xr fx] = fzero(f, 0)
[xrr fx] = fzero(f, 4)
[xmin, fmin]=fminbnd(f,1,2)
%PART 2
% line12>>
func= @(x,y) 2+x-y+2*x.^2+2*x.*y+y.^2;
[xgrid, ygrid]=meshgrid(-2:0.1:0,0:0.1:3);
subplot(1,2,2)
surf(xgrid,ygrid,func(xgrid,ygrid))
subplot(1,2,1)
contour(xgrid,ygrid,func(xgrid,ygrid))
subplot(1,2,2); surf(xgrid, ygrid, func(xgrid,ygrid))
f = @(x) 2+x(1)-x(2)+2*x(1)^2+2*x(1)*x(2)+x(2)^2
[x, fval]=fminsearch(f,[-.5, 0.5])
I leave it to you to determine if it produces the results you want.

2 件のコメント

Claudia Gurrola
Claudia Gurrola 2016 年 3 月 29 日
Thank you i do see how the order of my commands where off.
Star Strider
Star Strider 2016 年 3 月 29 日
My pleasure.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by