Fix error that says invalid first argument
2 views (last 30 days)
Show older comments
syms x y L
f = input('Enter the function f(x,y): '); % f(x,y)= x*y ; x^2+y^2
g = input('Enter the constraint function g(x,y): '); % g(x,y)= ((x^2)/8)+((y^2)/2)-1 ; x+y-10
F = f + L*g;
gradF = jacobian(F,[x,y]);
[L,x1,y1] = solve(g,gradF(1),gradF(2),'Real',true); % Solving only for Real x and y
x1 = double(x1); y1 = double(y1);
xmx = max(x1); xmn = min(x1); % Finding max and min of x-coordinates for plot range
ymx = max(y1); ymn = min(y1); % Finding max and min of y-coordinates for plot range
range = [xmn-3 xmx+3 ymn-3 ymx+3]; % Setting plot range
ezmesh(f,range);hold on; grid on;
h = ezplot(g,range); set(h,'LineWidth',2);
tmp = get(h,'contourMatrix');
xdt = tmp(1,2:end); % Avoiding first x-data point
ydt = tmp(2,2:end); % Avoiding first y-data point
zdt = double(subs(f,{x,y},{xdt,ydt}));
plot3(xdt,ydt,zdt,'-o','Color','b','MarkerSize',10);axis(range);
for i = 1:numel(x1)
G(i) = subs(f,[x,y],[x1(i),y1(i)])
plot3(x1(i),y1(i),G(i),'*k','MarkerSize',20);
end
title('Constrained Maxima/Minima')
2 Comments
Answers (1)
Rangesh
on 29 Nov 2023
Hi Aparjita,
I understand that you want to address the issue of an invalid first argument.
The error is occurring because the plot range is not in a valid syntax. If you comment out that section of the code, it should work fine. It seems like you are attempting to limit the axis of the plot. To achieve this, you can refer to the documentation on how to properly set the plot range:
I hope this resolves your query.
Thanks,
Rangesh.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!