Plot 3d graph with equation and variables

I have a function
ypred = 0.3969 - 0.0531*x1 -0.1386*x2 +0.0803*x1.^2 -0.0301*x2.^2 -0.0808*(x1.*x2)
the x1 and x2 values are:
x1 = [0;1;0.5;-0.5;-1;-0.5;0.5]
x2 = [0;0;0.866;0.866;0;-0.866;-0.866]
I plotted the graph using the code attached. This code doesn't seem to work that well because I tried for the same code on a graph that was previously obtained using the equation
ypred = 265 - 167.6*x1 -35.8*x2 +102.9*x1.^2 +47.9*x2.^2 +25.9*(x1.*x2)
It has a minimum point at 0.7940,0.1621. This is not obtained using the code..And the graph doesn't seem right either..

 Réponse acceptée

Star Strider
Star Strider le 12 Mar 2014
Modifié(e) : Star Strider le 13 Mar 2014
The first equation has a minimum far from your x1 and x2 ranges:
ypred1 = @(x) 0.3969 - 0.0531*x(1) -0.1386*x(2) +0.0803*x(1).^2 -0.0301*x(2).^2 -0.0808*(x(1).*x(2));
min_1 = fminsearch(ypred1, [0; 0])
produces:
min_1 =
95.2347e+039
511.6276e+039
The second equation produces a minimum about where you expected it:
ypred2 = @(x) 265 - 167.6*x(1) -35.8*x(2) +102.9*x(1).^2 +47.9*x(2).^2 +25.9*(x(1).*x(2));
min_2 = fminsearch(ypred2, [0; 0])
produces:
min_2 =
794.3380e-003
158.9462e-003
Is this what you want?
ADDED: Using the values for min_2, this seems correct for your second equation (that I called ypred2):
ypred2z = @(x1,x2) 265 - 167.6*x1 -35.8*x2 +102.9*x1.^2 +47.9*x2.^2 +25.9*(x1.*x2);
xvct = -0.5:0.1:2;
[X Y] = meshgrid(xvct, xvct);
Z = ypred2z(X,Y);
figure(1)
meshc(X, Y, Z)
hold on
plot3(min_2(1),min_2(2),val2,'*r')
hold off
xlabel('X_1')
ylabel('X_2')
zlabel('Y_{PRED}')
grid on

4 commentaires

Dilini
Dilini le 13 Mar 2014
Hi thanks a lot :). Everything seems to work except for min_1 = fminsearch(ypred1, [0; 0]). It says error in fminsearch..
Star Strider
Star Strider le 13 Mar 2014
Modifié(e) : Star Strider le 13 Mar 2014
I didn’t plot ypred_1 but I doubt it would be of any benefit. The ‘error’ it gives me is that it exceeded the maximum number of function evaluations. This isn’t really an error. When I use optimset to increase the number of iterations and function evaluations to 100000:
ypred1 = @(x) 0.3969 - 0.0531*x(1) -0.1386*x(2) +0.0803*x(1).^2 -0.0301*x(2).^2 -0.0808*(x(1).*x(2));
opts_1 = optimset('MaxFunEvals',100000, 'MaxIter',100000);
[min_1,fv_1] = fminsearch(ypred1, [-1000; -1000], opts_1)
it notes:
Exiting: Maximum number of iterations has been exceeded
- increase MaxIter option.
Current function value: -Inf
min_1 =
-3.2792e+153
-16.2997e+153
fv_1 =
-Inf
It gives positive values for x(1) and x(2) for zero or positive starting points:
min_1 =
3.2792e+153
16.2997e+153
fv_1 =
-Inf
Note that the function value ( fv_1 here ) is ‘-Inf’ in both situations. The problem is with the function. It’s falling off the edge of the world. It won’t get any more accurate with more function evaluations.
ADDED:
When I plotted ypred1 with this code, it revealed itself to be a saddle point centred at [0, 0]. It has neither a finite minimum or maximum.
ypred1z = @(x1,x2) 0.3969 - 0.0531*x1 -0.1386*x2 +0.0803*x1.^2 -0.0301*x2.^2 -0.0808*(x1.*x2);
xvct = -100:1:100;
[X Y] = meshgrid(xvct, xvct);
Z = ypred1z(X,Y);
figure(1)
meshc(X, Y, Z)
xlabel('X_1')
ylabel('X_2')
zlabel('Y_{PRED}')
grid on
Dilini
Dilini le 13 Mar 2014
Thanks a lot! It makes a lot of sense now :) :)
Star Strider
Star Strider le 13 Mar 2014
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Deep Learning Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by