How can I graph this Lagrange Equation?
7 views (last 30 days)
Show older comments
My code works and gives results, but there is a problem with the graph, it is blank, how can I fix it?
syms x1 x2 x3 y1 y2 lambda
g = x1+x2+x3+y1+y2 - 10;
L = 12*x1-x1^2+8*x2-x2^2+18*x3-3*x3^2+24*y1-y1^2+10*y2-5*y2^2+lambda*g;
Lx1 = diff(L,x1);
Lx2 = diff(L,x2);
Lx3 = diff(L,x3);
Ly1= diff(L,y1);
Ly2= diff(L,y2);
Llambda = diff(L,lambda);
s = solve([Lx1,Lx2,Lx3,Ly1,Ly2, Llambda])
Lopt = subs(L,[x1,x2,x3,y1,y2,lambda],[s.x1,s.x2,s.x3,s.y1,s.y2,s.lambda])
%solution is over. lets make a graph.
drawingrange=[-10,10];
fig=figure;
hold on;
set(fig,'defaultLineLineWidth',5);
axis equal;
%2D graphs
fcontour(Lopt,drawingrange);
h=gplot(g,drawingrange);
fprintf('number of solutions:%i\n', size(s.lambda,1));
for i=1:size(s.lambda,1)
fprintf('x1%i, x2%i, x3%i, y1%i, y2%i, lambda%i: %f\t;%f\t;%f\t;%f\t;%f\t;%f\t',...
i,i,i,i,i,i,double(s.x1(i)),double(s.x2(i)),double(s.x3(i)),...
double(s.y1(i)),double(s.y2(i)),double(s.lambda(i)))
end
1 Comment
Torsten
on 27 Oct 2022
How do you want to plot a function that depends on 5 or 6 independent variables ?
Accepted Answer
Jayant Gangwar
on 3 Nov 2022
Hi Sena,
"fcontour" function can be used to plot the contour lines of symbolic expression f(x,y), which means that it is suitable to plot a symbolic expression with no more than 2 independent variables, but you are trying to use it to plot an expression with 6 independent variables because of which it is outputting an empty graph.
To visualize your solution, it would require a 6D space which is not possible.
Please refer to this MATLAB answer for more information on how you can visualize your solution by breaking it into parts.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!