MATLAB: Does the programm do not run the script and continues to get unexpected expression

line 12unexpected matlab 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])

Best Answer

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.
Related Question