Integrating over a matrix of points [duplicate] - matlab

This question already has answers here:
How to integrate over a discrete 2D surface in MATLAB?
(3 answers)
Closed 8 years ago.
I have a matrix of points F(x,y) = z, but I have no expression for F(x,y).
x is from [0-2pi] and y is from [0-pi]. For each pair of "coordinates", I have a value of z.
I would like to perform a double integration from 0-2pi and 0-pi of F. Can I do this computationally (MatLab) without having an analytical expression?
Thanks!

Assuming that the (x,y) grid is uniform, you can approximate the integral by a 2D-Riemman sum as follows:
result = sum(z(:))*delta_x*delta_y;
where delta_x, delta_y are the grid spacings in the x and y directions. In your case these can be computed as
delta_x = 2*pi/numel(x); %// or 2*pi/(numel(x)-1)
delta_y = pi/numel(x); %// or pi/(numel(x)-1)
A perhaps more intuitive interpretation: compute the mean value of the function and multiply by the area of the (x,y) domain:
result = sum(z(:))/(numel(x)*numel(y)) * 2*pi^2; %// or replace numel(x)*numel(y)
%// by numel(z)

Related

X value for given y value [duplicate]

This question already has answers here:
Spline interpolation in matlab in order to predict value
(2 answers)
Extracting x value given y threshold from polyfit plot (Matlab)
(1 answer)
Closed 4 years ago.
i have a very simple question but i can't find my wrong. I have two signals, one same x axes and two signals value on y axis. I need to find the y value of green signal on the black line, so where y value of blue signal is 50.05. It should be between 6-7. I thought that i should first get same x value (4.676). Then get the y value of green signal where x is 4.676. So i need to get first 4.676 but i can't get this x value on given y value. I tried this but it comes always empty matrix.
xvalue = interp1(x_bluesignal,y_bluesignal, 50.05)
and
idx = find(x_bluesignal == 50.05);
Xidx = x_bluesignal(idx);
Any idea? Thank you!
As #obchardon pointed out in the comments, you want to interpolate on the x-value, not the y-value. As a simple example, consider the following:
%Plot two original lines
x = linspace(0,20,101);
y1 = 5*x+2;
y2 = 3*x+5;
plot(x, y1, 'b', x, y2 ,'g'); hold on
%Plot intersection line at desired y_interest value
y_interest = 50.05;
xvalue = interp1(y1, x, y_interest);
plot([xvalue, xvalue], [0, 100], 'k--')
x_interest = interp1(x, y2, xvalue);
This produces the following plot:
Once you've interpolated on x to find the correct x-value, you can then feed that value to your next interpolation on the y-values of the green curve. In the example above, this will output 33.8300.
In both your attempts you are actually asking MATLAB for the y value where x is 50.05, rather than your intended query.
Both
xvalue = interp1(y_bluesignal,x_bluesignal, 50.05)
and
idx = find(y_bluesignal == 50.05);
Xidx = x_bluesignal(idx);
may work if there is an entered data point at y=50.05 (use a tolerance as in the above comments if this is a calculated value), the interp method will work regardless

How to draw a surface in matlab by using three vectors? [duplicate]

This question already has answers here:
How can I plot a function with two variables in Octave or Matlab?
(3 answers)
Closed 3 years ago.
I have three vectors:
a (1500 x 1)
b (1500 x 1)
c (1500 x 1)
When I use surf(a, b, c) then it gives the following error:
Error using surf (line 57)
Z must be a matrix, not a scalar or vector.
I have to draw the surface for these three vectors. How can i do this?
That is not clear how you try to draw surface: if you mean a is a first dimension (1500*1), b - the second (1500*1), so third dimension have to be (1500*1500) - values for each point of grid a*b.
The solution: you have to form 1 of them of 1500*1500 size:
a = 1:1500;
b = 1:1500;
c = ones(1500);
surf(a,b,c);

I want to draw ellipse in matlab. I have ellipse parameters [duplicate]

This question already has an answer here:
Draw ellipse in MATLAB
(1 answer)
Closed 6 years ago.
Any ellipse can be uniquely defined by five parameters i.e. center x0 and y0, semi-major aixs length a, semi minor axis length b, and orientation angle theta. I have parameters x0, y0, a, b, and theta. How can I exactly draw the ellipse?
Some researches are necessary before to ask this kind of question. Mainly if the question was asked too much time.
You can do something like this :
Let (x1,y1) and (x2,y2) be the coordinates of the two vertices of the ellipse's major axis, and let e be its eccentricity.
a = 1/2*sqrt((x2-x1)^2+(y2-y1)^2);
b = a*sqrt(1-e^2);
t = linspace(0,2*pi);
X = a*cos(t);
Y = b*sin(t);
w = atan2(y2-y1,x2-x1);
x = (x1+x2)/2 + X*cos(w) - Y*sin(w);
y = (y1+y2)/2 + X*sin(w) + Y*cos(w):
plot(x,y,'y-')
axis equal
I don't have time to test it, but it should work.
Next time, please read this section : How do I ask a good question

Create a 3D matrix with the 3rd dimension being a random distribution [duplicate]

This question already has answers here:
How to create a random 3D matrix?
(2 answers)
Closed 7 years ago.
I am trying to create in MATLAB a 3D matrix (say, x,y,z). The x and y dimensions represent a 2D location (addressable by index x and y) and z should represent a vector containing random numbers of a normal distribution.
For example, if i were to extract the vector (3,5,z), it would give me all the random values in the z dimension at the location (3,5). x and y should both be of size 100 and z should be of size 1000.
You can do something like:
z = 1:1000;
A = rand(100, 100, numel(z));
rvec = reshape(A(3, 5, z), [numel(z), 1]);
Here, rvec is the random vector you are looking for.

How to plot an cylindrical coordinates equation in cartesian [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have this equation r=z*cos(theta) and I need to plot it in Cartesian coordinates in Matlab. How can I do this?
First off, the definition of your cylindrical co-ordinates is wrong. Given the azimuthal sweep around the z axis theta as well as the radius of the cylinder r, the Cartesian co-ordinates within a cylinder is defined as:
x = r*cos(theta)
y = r*sin(theta)
z = z
Therefore, you would need to define a grid of co-ordinates for r, theta and z, use these co-ordinates and plug them into the above code, then draw them. I would recommend you use plot3 as you want to plot 3D points in Cartesian space. Also, use meshgrid to define your grid of points.
As such, the radius of a cylinder is (usually) constant when you're drawing it, and theta and z are the quantities you are varying. Let's assume that -2 <= z <= 2 and r = 2. We know that to create a cylinder, we have to sweep around a circle from 0 <= theta <= 2*pi. Therefore, do something like this:
%// Define (r,theta,z)
[theta, z] = meshgrid(0:0.001:2*pi, -2:0.001:2);
r = 2*ones(size(theta));
%// Calculate x and y. z was calculated earlier
x = r.*cos(theta);
y = r.*sin(theta);
%// Plot the points
plot3(x(:), y(:), z(:), 'b.');
grid;
view(-48,60); %// Adjust viewing angle
This is what I get:
You can certainly use more efficient techniques, like what Kamtal has suggested with pol2cart, but you said you wanted the actual Cartesian co-ordinates, and so x, y and z contains those co-ordinates in 3D space for you. I'm assuming you want these for further processing.
Good luck!
n = linspace(-pi,pi,20);
m = linspace(0,1,20);
[theta,z] = meshgrid(n,m);
r = z .* cos(theta);
[X,Y,Z] = pol2cart(theta,r,z);
surf(X,Y,Z)
axis equal
If you change it to r = 1 .* cos(theta); then you will get a cylinder,