plot a graph in matlab (Matrix dimensionions must agree) - matlab

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);

Related

MATLAB: polyval function for N greater than 1

I am trying trying to graph the polynomial fit of a 2D dataset in Matlab.
This is what I tried:
rawTable = readtable('Test_data.xlsx','Sheet','Sheet1');
x = rawTable.A;
y = rawTable.B;
figure(1)
scatter(x,y)
c = polyfit(x,y,2);
y_fitted = polyval(c,x);
hold on
plot(x,y_fitted,'r','LineWidth',2)
rawTable.A and rawTable.A are randomly generated numbers. (i.e. the x dataset cannot be represented in the following form : x=0:0.1:100)
The result:
second-order polynomial
But the result I expect looks like this (generated in Excel):
enter image description here
How can I graph the second-order polynomial fit in MATLAB?
I sense some confusion regarding what the output of each of those Matlab function mean. So I'll clarify. And I think we need some details as well. So expect some verbosity. A quick answer, however, is available at the end.
c = polyfit(x,y,2) gives the coefficient vectors of the polynomial fit. You can get the fit information such as error estimate following the documentation.
Name this polynomial as P. P in Matlab is actually the function P=#(x)c(1)*x.^2+c(2)*x+c(3).
Suppose you have a single point X, then polyval(c,X) outputs the value of P(X). And if x is a vector, polyval(c,x) is a vector corresponding to [P(x(1)), P(x(2)),...].
Now that does not represent what the fit is. Just as a quick hack to see something visually, you can try plot(sort(x),polyval(c,sort(x)),'r','LineWidth',2), ie. you can first sort your data and try plotting on those x-values.
However, it is only a hack because a) your data set may be so irregularly spaced that the spline doesn't represent function or b) evaluating on the whole of your data set is unnecessary and inefficient.
The robust and 'standard' way to plot a 2D function of known analytical form in Matlab is as follows:
Define some evenly-spaced x-values over the interval you want to plot the function. For example, x=1:0.1:10. For example, x=linspace(0,1,100).
Evaluate the function on these x-values
Put the above two components into plot(). plot() can either plot the function as sampled points, or connect the points with automatic spline, which is the default.
(For step 1, quadrature is ambiguous but specific enough of a term to describe this process if you wish to communicate with a single word.)
So, instead of using the x in your original data set, you should do something like:
t=linspace(min(x),max(x),100);
plot(t,polyval(c,t),'r','LineWidth',2)

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!

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.

Creating a 3D function in Matlab using meshgrid

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))

Matlab plot of a function with 2 variables

I have a problem to plot a function with 2 variables:
if i do:
x= linspace(0,5);
y=linspace(0,5);
[x,y]=meshgrid(x,y);
z=log(x.*sqrt(y-x));
mesh(x,y,z);
I get this error:
Error using mesh (line 76) X, Y, Z, and C cannot be complex.
I think because I have some complex results in the computation.
How could I solve?
What kind of output do you expect? It's possible to plot the absolute value using mesh(x,y,abs(z));, but I'm not sure if this is what you want. quiver is another possibility to plot your data.
The reason you are getting complex results is the sqrt(y-x) part in your code. y is less than x at almost half of the points of your grid which leads to computing the square root of a negative number.
So, as Daniel suggested, you can use abs(z). Alternatively you could check if it is OK for the particular application to compute sqrt(abs(y-x)) to make sure you have a positive number under the square root