Creating a 3D function in Matlab using meshgrid - matlab

This should be a really simple question, but for some reason I'm getting unreasonably confused and the Matlab documentation isn't helping.
Given a uniform grid of coordinates (x_i,y_j,z_k), I want to make a 3-dimensional array F in Matlab such that F(i,j,k)=f(x_i,y_j,z_k). The following is obviously incorrect:
x=linspace(-1,1,100) % uniform mesh on [-1,1]^3
[X,Y,Z]=meshgrid(x);
f=X.*Y.*sin(pi*Y.*Z) % for example
Do I need to use permute somewhere? I know that I could simply make a triple loop, but as we know that is slow.
Thanks!

Use ndgrid instead of meshgrid to avoid the unwanted permutation between first and second dimensions.
From the documentation (see also here):
MESHGRID is like NDGRID except that the order of the first two input
and output arguments are switched (i.e., [X,Y,Z] = MESHGRID(x,y,z)
produces the same result as [Y,X,Z] = NDGRID(y,x,z))

Related

plot a graph in matlab (Matrix dimensionions must agree)

I am new to matlab and I am having the difficulty: I would like to have a graph of a function plotted and 'r' signifies the parabolic equation and valueof 'y' varies and while adding to 'k' it is showing error.
the code is shown below`clear all;
x=[3,4,5,6,7,8,9,10,11,10,13,14,15,16,17,18,19,20,21,22,23];
a=(8.854.*(10.^-12).*(0.016));
y=-0.0925:0.01:0.0925
z=(0.03);
r=((7.3.*(y).^2)+(z));
k=((x.*10^-2))+((r))
c=(a./k);
plot(x,c);
and the error in command window is
Error using +
matrix dimensions must agree.
error in program(line 8)
k=((x.*10^-2))+((r))
how can I get around this problem ?
As people have pointed out in the comments, the matrix dimensions are the issue. If you make x the same dimensions as y (i.e., 1x19), as seen below, you will produce a graph, which is what it sounds like you want:
x=3:21;
a=(8.854.*(10.^-12).*(0.016));
y=-0.0925:0.01:0.0925
z=(0.03);
r=((7.3.*(y).^2)+(z));
k=((x.*10^-2))+((r))
c=(a./k);
plot(x,c);
From your comment: If you want it in 2-D, i.e., one value of k for every pair of x and r then in Matlab 2016a+, all you need is k=((x.*10^-2))+((r).'), i.e., one more transpose. In Pre-2016, you would use arrayfun for this:
[xr,rx] = meshgrid(x,r);
k = arrayfun(#(x,r) ((x.*10^-2))+((r)),xr,rx);
Btw, to plot a 2-D image you would not use plot but imagesc. This is what it looks like:
First: your variable k, second: your variable c. Is this what you were looking for?
As pointed out by Matlab and in the comments, the problem is indeed the difference in size of the vector.
you can either change x or a better solution is to use linspace , as :
y=linspace(-0.925,0.0915,21);

Create diagonal matrix without using MATLAB built-in functions

I know the answer to this question as shown below.
function a = reverse_diag(n)
b = zeros(n);
b(1:n+1:end) = 1;
a(1:n, n:-1:1) = b(1:n, 1:n);
end
But why does it look like that? What does this mean?
b(1:n+1:end) = 1;
I seem to recall seeing something similar to this in MATLAB answers very recently, hence I will be brief.
MATLAB arrays can be indexed in 2 relevant ways:
Like A(x,y) using the actual coordinates in the matrix.
Like A(index), no matter how many dimensions the matrix A actually has. This is called linear indexing and will go through the matrix column by column. So for a 10x10 matrix, A(11) is actually A(2,1).
Read up on ind2sub and sub2ind to get a sense of how these work. You should now be able to figure out why that line works.

i have 100*100 matrix, how can i make plot3 graph?

I have a 100 x 100 matrix and i have to use plot3 in MATLAB environment to graph this data. I tried plot3(matrix name) but I faced this error "not enough input arguments". I think plot3 needs 3 input arguments, but I only have this matrix of data. could anyone help me to solve this problem? Is there any alternative for plot3 when we don't have enough arguments?
I need a graph like this:
I think you want to plot the values in a figure as a sort of surface element. What you can do then is:
[X,Y] = size(matrix);
figure;
surface(1:X,1:Y,matrix);
What this does is that it creates a vector for both X and Y indices, as possible in surface. The X and Y indices are obtained by setting them as integers from 1:size, so basically you assign the location of each matrix element to an index.
Note that you can strictly speaking use surface(matrix) as well, but the former approach allows you to use custom indexing, as long as the lengths of the vectors X and Y are the same as the size of your matrix.
For the waterfall use:
figure;
waterfall(matrix);
Sample code:
A=rand(100);
figure;
waterfall(1:100,1:100,A);
Gives:
where you can play around with the name-value pairs, see the documentation on that.
I think what you need is mesh or surf instead of plot3.
plot3 draws a line in 3d-space, so it will need three vectors of the same length (one for each dimension).
When you have a matrix, one reasonable way of displaying it is as a surface in 3d space, which is done by the functions mesh and surf.
Try it out! I hope i helps!

Matlab : Intersect point of curves

Say for example I have data which forms the parabolic curve y=x^2, and I want to read off the x value for a given y value. How do I go about doing this in MATLAB?
If it were a straight line, I could just use the equation of the line of best fit to calculate easily, however I can't do this with a curved line. If I can't find a solution, I'll solve for roots
Thanks in advance.
If all data are arrays (not analytical expressions), I usually do that finding minimal absolute error
x=some_array;
[~,ind]=min(abs(x.^2-y0))
Here y0 is a given y value
If your data are represented by a function, you can use fsolve:
function y = myfun(x)
y=x^2-y0
[x,fval] = fsolve(#myfun,x0,options)
For symbolic computations, one can use solve
syms x
solve(x^2 - y0)
Assuming your two curves are just two vectors of data, I would suggest you use Fast and Robust Curve Intersections from the File Exchange. See also these two similar questions: how to find intersection points when lines are created from an array and Finding where plots may cross with octave / matlab.

Plotting from 3D matrix in Matlab

I have a matrix which is 1*1*10000, the slightly odd dimensions are the result of the matrix algebra used to calculate it.
I simply want to be able to plot the 10000 data points contained in it, but matlab seems unable to do it?
Can someone please tell me how I can plot the data?
Seems simple but I really can't figure out how to do it!
Baz
yes you need to reduce the dimensions to a vector:
A = zeros(1,1,100)
vector = squeeze(A(1,1,:))
as when you'd access the third dimension this would only return a 3D-Matrix again:
z = A(1,1,:)
would NOT work. So use squeeze() ;-) Then plot as usual.
Doc-Link: http://www.mathworks.de/de/help/matlab/ref/squeeze.html
And as Ander pointed out in comments, no need to give any dimensions, as it removes singleton-dimensions by itself. So just use vector = squeeze(A). MATLAB recognizes the way to go itself.