Finding minima and maxima of a surface in MATLAB - matlab

I have just started using MATLAB. I want to find the minima and maxima of this 2nd degree polynomial surface.
f(x,y) = 4490 + 8902*x + 1.385e+05*y + 9.497*x^2 + -9928*x*y + 1.481e+05*y^2;
I know how to calculate minima and maxima for a degree 1 function but I wasn't able to get any lead regarding higher degree functions. Any suggestions would be appreciated.

Evaluate the function for x and all y and find the min/max of that matrix:
x1D = 1:100;
y1D = 1:100;
[x,y] = meshgrid(x1D,y1D);
f = 4490 + 8902*x + 1.385e+05*y + 9.497*x.^2 + -9928.*x.*y + 1.481e+05*y.^2;
[minV, minL] = min(f(:));
[maxV, maxL] = max(f(:));
[minX, minY] = ind2sub(size(f),minL);
[maxX, maxY] = ind2sub(size(f),maxL);

Lets first define a range for x,y which you want to find minima.Then make a mesh grid which is basically a matrix containing those x and y as coordinates. Then evaluate the function at all those points as z which is also a matrix. Each member of z is at the position similar to x and y as input. Then find the location of minimum value for z matrix. The last 2 command should give you the position coordinates where the minima happened.
x=1:100;
y=1:100;
[X,Y]=meshgrid(x,y); %
z = 4490 + X.*8902 + Y.*1.385e+05 +X.^2*9.497 X.*Y.*9928+Y.^2*1.48e+05;
[X,Y]=meshgrid(x,y);
surf(x,y,z) % plotting the curve
[Row,Col]=find(z==min(min(z)))
x(Row) % x coordinate
y(Col) % y coordinate

Related

How plot a two-dimensional function in three-dimensional space with MATLAB?

I want to plot a two-dimensional function of the polar coordinates r and theta in three-dimensional cartesian coordinates. I have that (sorry about bad maths formatting, LaTeX not compatible, it seems)
f(r,theta) = r/2 * (cos(theta - pi/4) + sqrt(1 + 1/2 * cos(2*theta)))
Converting r and theta to cartesian coordinates
x = r * cos(theta), y = r * sin(theta)
Further, the domain is -1<r<1 and 0<theta<2 * pi, which I define by
r = -1:2/50:1;
and
theta = 0:2*pi/50:2*pi;
giving me two vectors of the same dimensions.
I can define the x and y values used for plotting as row vectors by
x = r. * cos(theta);
and
y = r. * sin(theta);
So now I need to define the z values, which will depend on the values of x and y. I thought I should make a 101x101 where each matrix element contains a data point of the final surface. But how should I do this? I thought about using a double for loop:
for i=1:numel(r)
for j=1:numel(theta)
z(i,j) = r(i)/2 .* cos(theta(j) - pi/4) + r(i).*sqrt(1 + 1/2 * cos(2.*theta(j)));
end
end
Then simply surf(z)
While this definitely gives me a surface, it gives me the incorrect surface! I don't know what is happening here. The incorrect surface is given in Figure 1, while the correct one is given in Figure 2. Can anyone help me out? For reference, the correct surface was plotted with GeoGebra, using
A = Function[<expression 1>, <Expresison 2>, <Expression 3>, <var 1>, <start>, <stop>, <var 2>, <start>, <stop>]
Figure 1. Incorrect surface.
Figure 2. Correct surface.
As others have said, you can use meshgrid to make this work.
Here's your example using gridded r and theta and an anonymous function to replace the double loop:
r = -1:2/50:1;
theta = 0:2*pi/50:2*pi;
% define anonymous function f(r,theta)
f = #(r,theta) r/2 .* (cos(theta - pi/4) + sqrt(1 + 1/2 .* cos(2.*theta)));
% generate grids for r and theta
[r, theta] = meshgrid(r,theta);
% calculate z from gridded r and theta
z = f(r,theta);
% convert r,theta to x,y and plot with surf
x = r.*cos(theta);
y = r.*sin(theta);
surf(x,y,z);
You need to use meshgrid to get matrix coordinates if you want to use surf. Taking your x and y (lower case), call
[X,Y] = meshgrid(x,y);
Then X and Y (upper case) will have the same values as you gave it, but laid out in the two-dimensional array as expected by surf. Loop over the indices here and compute your Z, which should have all(size(Z) == size(X)).
https://www.mathworks.com/help/matlab/ref/meshgrid.html

how to interpolate a fluctuated vector in matlab?

I have two arrays
x = [0 9.8312 77.1256 117.9810 99.9979];
y = [0 2.7545 4.0433 5.3763 5.0504];
figure; plot(x, y)
I want to make more samples of x and y then I interpolated both arrays. I tried this code
xi =min(x):.1:max(x);
yi = interp1(x,y,xi);
figure; plot(xi, yi)
but the trajectory is not same as previous plot. Its because the xi is not fluctuating same as x. How should I interpolate both arrays with same trajectory as original one?
This is an issue because when interpolating, MATLAB is going to ignore the order that you feed in the points and instead just sort them based upon their x location.
Rather than interpolating in x/y coordinates, you can instead use a parameter which represents the cumulative arc length of the line segments and use that to interpolate both the x and y coordinates. This will provide you with an interpolant that respects the order and guarantees monotonicity even for multiple values at the same x coordinate.
% Compute the distance between all points.
distances = sqrt(diff(x).^2 + diff(y).^2);
% Compute the cumulative arclength
t = cumsum([0 distances]);
% Determine the arclengths to interpolate at
tt = linspace(t(1), t(end), 1000);
% Now interpolate x and y at these locations
xi = interp1(t, x, tt);
yi = interp1(t, y, tt);

Contour plot of a function of 3 variables

As we can make contour plot of f=(x.^2) + (y.^2); in 2-D as follows:
[x,y]= meshgrid(-10:10, -10:10);
contour(x,y, (x.^2)+(y.^2));
and we can make contour plot of f=(x.^2) + (y.^2); in 3-D using contour3
Can we make contour plot of f=(x.^2) + (y.^2) + (z.^2); in 3-D
The matlab function isosurface can do what you are asking. However you can also achieve the results you want using other alternatives, like using surf. I will cover both methods.
Method 1: Using isosurface
We need to create the domain for x, y and z and then generate a 3D mesh with those values so that we can evaluate the function f(x,y,z) = x^2 + y^2 + z^2. Then, we shall give a value to the constant k and feed all this information to isosurface, so that we can obtain the family of (x,y,z) values that satisfy the condition: f(x,y,z) = k. Note that this contours are in fact spheres! Finally we can use patch to generate a surface with those values.
It is very interesting to give different values for k iterativelly and see the contours associated with those values.
% Value for x, y and z domain.
a = 10;
% Domain for x ,y and z.
x = linspace(-a,a);
y = linspace(-a,a);
z = linspace(-a,a);
% Generate a 3D mesh with x, y and z.
[x,y,z] = meshgrid(x,y,z);
% Evaluate function (3D volume of data).
f = x.^2 + y.^2 + z.^2;
% Do contours from k = 0 to k = 100 in steps of 1 unit.
for k = 0:100
% Draw the contour that matches k.
p = patch(isosurface(x,y,z,f,k));
isonormals(x,y,z,f,p)
p.FaceColor = 'red';
p.EdgeColor = 'none';
% Adjust figure properties.
title(sprintf('Contours of f(x,y,z) = x^2 + y^2 + z^2\nwith f(x,y,z) = k = %d',k));
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
axis equal;
grid on;
box on;
axis([-10 10 -10 10 -10 10]);
camlight left;
lighting phong;
% Update figure.
drawnow;
% Clear axes.
cla;
end
This is the output:
Method 2: Using surf
As in the previous method, to contour the function f(x,y,z) = x^2 + y^2 + z^2, we need to match the function to a constant value, this is f(x,y,z) = k, where k is any constant value you choose.
If we isolate z in terms of k, x and y we have: z = ± sqrt(k-x.^2-y.^2), so we have the explicit values for x, y and z. Now, let's give different values for k iterativelly and see the results that we get with the surf function!
% Do contours from k = 0 to k = 100 in steps of 1 unit.
for k = 0:100
% Find the value where: k - x^2 - y^2 = 0
a = sqrt(k);
% Domain for x and y.
x = linspace(-a,a);
y = linspace(-a,a);
[x,y] = meshgrid(x, y);
% Isolate z in terms of k, x and y.
z = sqrt(k-x.^2-y.^2);
% Find complex entries in z.
i = find(real(z)~=z);
% Replace complex entries with NaN.
z(i) = NaN;
% Draw upper hemisphere of surface.
surf(x,y,z,'FaceColor','red','EdgeColor','none');
hold on;
% Draw lower hemisphere of surface.
surf(x,y,-z,'FaceColor','red','EdgeColor','none');
% Adjust figure properties.
title(sprintf('Contours of f(x,y,z) = x^2 + y^2 + z^2\nwith f(x,y,z) = k = %d',k));
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
axis equal;
grid on;
box on;
axis([-10 10 -10 10 -10 10]);
camlight left;
lighting phong;
% Update figure.
drawnow;
hold off;
end
This is the output:
References
I took some of the ideas from David Arnold's article "Complex Numbers and Plotting in Matlab", which is well worth a read and will help you understand how to plot functions that generate complex numbers.

matlab - Full Multivariate polynomial function plot

hi I have a question related to full polynomial function of two variables. I can write code to calculate the value of it but I dont know how to plot the contour at level 0. For example: given a full 5th-order polynomial
f(x,y) = theta1 + theta2*x + theta3*y + theta4*x*y + theta5*x^2 + theta6*y^2 + ... + theta20*x^5 + theta21*y^5
I already wrote the code to compute f(x,y) given theta, x and y but how can I plot this function
theta1 + theta2*x + theta3*y + theta4*x*y + theta5*x^2 + theta6*y^2 + ... + theta20*x^5 + theta21*y^5 = 0 ?
thank you!
I'm going to assume that your function takes values element wise. If not, declare a function handle that takes in the variables x and y that represent the values in those respective dimensions:
f = #(x,y) theta1 + theta2*x + theta3*y + theta5*x.^2 + theta6*y.^2 + ... + theta24*x.^5 + theta25*y.^5;
(.... do you need to have that many theta values?). Next, generate a domain of x and y coordinates that you wish to plot. You can do that with meshgrid:
[X,Y] = meshgrid(-10:0.01:10);
This generates a grid of (x,y) coordinates between -10 to 10 for both dimension in steps of 1.
Now, generate your Z coordinates by using the above function:
Z = f(X,Y);
Now that you're finally done, use the contour function with X,Y and Z. Now, if you want to show just the level where f(x,y) = 0, you specifically need to do this:
contour(X,Y,Z,[0 0]);
In general, the fourth element takes in a vector of contour levels but if you want to specifically draw one level, that's what you need to do.

find polynomial equation from array with matlab

I've some x.mat and y.mat.
And I would like to find the polynomial equation from this.
I've tried
p = polyfit(x,y,3);
with y2 = p(1)*x.^3 + p(2)*x.^2 + p(3)*x
but my y2 is not equal to the original y . What is wrong ?
Thanks you
As radarhead wrote in his comment, you forgot the coefficient of zero degree (p(4) here).
Assuming x and y are vectors of same length n, polyfit(x,y,n-1) will return a vector containing the coefficients of the interpolating polynomial (of degree n-1) in descending order.
Then, the value of the interpolating polynomial at a point z will be given by:
p(1)*z^3 + p(2)*z^2 + p(3)*z + p(4)
Don't forget p(4)! As Bas suggested, you can use the polyval function to easily compute the value of a polynomial at a a given point:
polyval(p,z);
To illustrate this, see the code below, which generates 4 data points, plots those points and the polynomial interpolating them:
n = 4;
x = sort(rand(n,1));
y = rand(n,1);
p = polyfit(x,y,n-1);
figure
hold on
plot(x,y,'bo');
xx=linspace(x(1),x(end),100);
plot(xx,polyval(p,xx),'r');
hold off