How to plot 3D suraface and contour lines?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
ANCY S GEORGE
am 2 Jun. 2022
Beantwortet: Bjorn Gustavsson
am 2 Jun. 2022
I want to plot the function
X0=[0;0];
lb=[-10;10];
ub=[-10;10];
f=@(x)(2*x(1)+20/x(1)+2*x(2)+20/x(2)+120);
x=fmincon(f,X0,[],[],[],[],lb,ub)
How to plot the 3D surface and also the contour lines?
0 Kommentare
Akzeptierte Antwort
Bjorn Gustavsson
am 2 Jun. 2022
To effectively use matlab sometimes one has to make functions vectorized - such that you can feed the function arrays and get all results at once. This is such a case. Your f works well for the optimization. But if you define it this way:
f=@(x1,x2)(2*x1+20./x1+2*x2+20./x2+120);
You can still run it through fmincon:
X0=[1;1]; % Note that your function is undefined at [0, 0] due to the 2 1/x-terms
lb=[-10;-10]; % Note that I've changed the bounds. Yours constrained the solution to be
ub=[10;10]; % X1 = -10 and X2 = 10
x=fmincon(@(X) f(X(1),X(2)),X0,[],[],[],[],lb,ub);
and then effectively plot the function with surf and contour:
[x1,x2] = meshgrid(linspace(lb(1),ub(1),100),linspace(lb(2),ub(2),100));
surf(x1,x2,f(x1,x2)),shading flat
hold on
contour(x1,x2,f(x1,x2),-200:30:400)
HTH
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Contour Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!