I have a matrix
b = [1+ 1i, 2 + 1i, 2+ 2i, 3 + 3i, 3+ 3i ; ...
1.2 + 2i , 2+2i, 2.1 + 2.1i, 3+2.1i, 3.1 + 3.2i]
where real(b) is the x coordinate, b(x,:) is one experiment, and imag(b) is the y coordinate.
I want two things:
plot my experiments in a 2d plot as lines (but the points have to be in the right order)
plot my y (usually calledz) coordinate as a surface over the axes x and experiment.
The problem is, that I want lines along the rows and Matlab mixes the coordinates of the complex numbers up and the line appears in a zig-zag all over the place.
The more basic problem is that I want to have bars from x1 to x2 at y1 and I only came up with adding a data point y1 at x1 and x2. But at x2 there is also y2 which seems to confuse Matlab.
You can use Euler's formula to convert your data from Cartesian coordinates to polar coordinates.
clear all; close all;
function [rho, theta] = polarize(z)
rho = abs(z);
theta = angle(z);
end
b = [1+ 1i, 2 + 1i, 2+ 2i, 3 + 3i, 3+ 3i;
1.2 + 2i , 2+2i, 2.1 + 2.1i, 3+2.1i, 3.1 + 3.2i];
[rho1, theta1] = polarize(b(1,:));
[rho2, theta2] = polarize(b(2,:));
figure
hold on
polar(theta1, rho1, 'b');
polar(theta2, rho2, 'r');
print('-dpng','euler.png')
Result in Octave:
For question (1), plot(b) is going to give you lines made up of the columns of b. If you switch to using b-transpose, i.e. plot(b'), you'll plot each row separately.
plot(b')
ylim([-4 0])
xlim([-0 4])
Question (2) requires a certain toolbox for the resp function?
Related
I am not familiar with MATLAB environment and I want to draw a matrix in a way that presents each cell of a matrix as a point in the 3D space.
For example present matrix "A " with the following points in 3D space:
x=1, y=1 Z= 10 , x=1, y=3 Z=27 until reach the point x=3, y=3, z=26.
A =
10 15 27
56 87 2
90 87 26
I do not want to use mesh and surf. I am looking for diagram like plot3 digram.I tried plot3 but it does not show the value of z correctly.
i=1:3;
j=1:3;
plot3(i,j,A(i,j))
In the above figure; 3 values are presented for when x=3 and y=3 however it should present these values for x=1,y=3; x=2,y=3;x=3,y=3
To begin I would recommend you to change your variable names. I'll use x and y instead of i and j. In many language these symbols are more typically used for scalar index rather than full vector indices, and in Matlab they can have a special significance (they are used to represent complex numbers).
That said, in your statement i=1:3; you only generate 3 indices, but you have 9 values to plot in your matrix. These 3 indices have to be repeated (3 times in your case, one for each column). So a proper x and y generation would be:
%% // Manual mesh/coordinate generation
x = bsxfun(#times,1:size(A,1), ones([size(A,2) 1])) ;
x = x(:) ;
y = bsxfun(#times, ones([1 size(A,1)]) , (1:size(A,2)).' ) ;
y = y(:) ;
With that you can use at your convenience scatter3 or plot3:
hscat = scatter3( x, y, A(:) ) ;
hp3 = plot3( x, y, A(:),'Marker','o','LineStyle','none') ;
%// will both produce exactly the same result
Now please consider the fact that the way I generated the x and y coordinate is nothing more than what meshgrid would do for you (or the more dimension generic ndgrid).
For example, in the code below, the 3 plotting methods will produce exactly the same ouput than above, so just take your pick:
%% define a grid
[X,Y] = meshgrid( 1:size(A,1) , 1:size(A,2) ) ;
%% // surface plot (but only points visible, no line)
hsurf = surf(X,Y,A,'Marker','o','LineStyle','none','FaceColor','none') ;
%% // scatter3
hscat = scatter3( X(:), Y(:), A(:) ) ;
%% // plot3
hp3 = plot3( X(:), Y(:), A(:),'Marker','o','LineStyle','none') ;
Why reinvent the wheel when you have one at hand ... meshgrid do the job for you in less code instruction ;-)
To generate the coordinates, you should use ndgrid (or meshgrid, which swaps X and Y.):
[X, Y] = ndgrid(1:3, 1:3);
plot3 is going to connect the input points with a line, so if you want distinct points in your plot, use scatter3:
scatter3(X(:), Y(:), A(:));
(You can also use plot3 in the same way if you want the lines.)
How can I make a simple plot of function y = sin(x) + sin(3x) + ... + sin(100x) without using any loops?
Yes, it's possible by using a call to bsxfun to generate the right points to be applied per sinusoid, then using a sum call to sum all of the sinusoids for each point. You'd then plot this normally.
Something like this comes to mind:
x = -5:0.01:5; %// Define x points here
pts = bsxfun(#times, 1:2:101, x(:)); %// Generate a grid of points
y = sum(sin(pts), 2); %// Compute the y values for each x value
plot(x(:),y); %// Plot the result
The first line of code generates a set of x values that you wish to plot. The next line of code generates a 2D grid of points. Each row applies x, 3*x, 5*x, ..., 101*x for one particular point in x. Each column represents one unique x point. As such, when we use sum (next line), we also apply the sin operator to each of these individual points on the grid, then go ahead and sum over each row to produce the output for each unique point of x. We then plot the results.
Note that I used x(:) to unroll the x vector so that it's a column vector. This is needed for the code to work. This also allows you to make x a row or column vector and the code will still work.
This is the plot I get:
Use cumsum.
octave:1> x = 1;
octave:2> sin(x)
ans = 0.841470984807897
octave:3> sin(x*1)
ans = 0.841470984807897
octave:4> sin(x*1) + sin(x*2)
ans = 1.75076841163358
octave:5> sin(x*1) + sin(x*2) + sin(x*3)
ans = 1.89188841969345
octave:6> cumsum(sin(x * (1:3)))
ans =
0.841470984807897 1.75076841163358 1.89188841969345
I want to draw a contour plot for 3D data.
I have a force in x,y,z directions I want to plot the contour3 for that
the dimensions of the Fx = 21x21X21 same for Fy and Fz
I am finding force = f*vector(x,y,z)
Then
Fx(x,y,z) = force(1)
Fy(x,y,z) = force(2)
Fz(x,y,z) = force(3)
I did the following but it is not working with me ?? why and how can I plot that
FS = sqrt(Fx.^2 + Fy.^2 + Fz.^2);
x = -10:1:10;
[X,Y] = meshgrid(x);
for i=1:length(FS)
for j = 1:length(FS)
for k=1:length(FS)
contour3(X,Y,FS(i,j,k),10)
hold on
end
end
end
This is the error I am getting
Error using contour3 (line 129)
When Z is a vector, X and Y must also be vectors.
Your problem is that FS is not the same shape as X and Y.
Lets illustrate with a simple example:
X=[1 1 1
2 2 2
3 3 3];
Y=[1 2 3
1 2 3
1 2 3];
Z=[ 2 4 5 1 2 5 5 1 2];
Your data is probably something like this. How does Matlab knows which Z entry corresponds to which X,Y position? He doesnt, and thats why he tells you When Z is a vector, X and Y must also be vectors.
You could solve this by doing reshape(FS,size(X,1),size(X,2)) and will probably work in your case, but you need to be careful. In your example, X and Y don't seem programatically related to FS in any way. To have a meaningful contour plot, you need to make sure that FS(ii,jj,k)[ 1 ] corresponds to X(ii,jj), else your contour plot would not make sense.
Generally you'd want to plot the result of FS against the variables your are using to compute it, such as ii, jj or k, however, I dont know how these look like so I will stop my explanation here.
[ 1 ]: DO NOT CALL VARIABLES i and j IN MATLAB!
I'm not sure if this solution is what you want.
Your problem is that contour and contour3 are plots to represent scalar field in 2D objects. Note that ball is 2D object - every single point is defined by angles theta and phi - even it is an object in "space" not in "plane".
For representation of vector fields there is quiver, quiver3, streamslice and streamline functions.
If you want to use contour plot, you have to transform your data from vector field to scalar field. So your data in form F = f(x,y,z) must be transformed to form of H = f(x,y). In that case H is MxN matrix, x and y are Mx1 and Nx1 vectors, respectively. Then contour3(x,y,H) will work resulting in so-called 3D graph.
If you rely on vector field You have to specify 6 vectors/matrices of the same size of corresponding x, y, z coordinates and Fx, Fy, Fz vector values.
In that case quiver3(x,y,z,Fx,Fy,Fz) will work resulting in 6D graph. Use it wisely!
As I comment the Ander's answer, you can use colourspace to get more dimensions, so You can create 5D or, theoretically, 6D, because you have x, y, z coordinates for position and R, G, B coordinates for the values. I'd recommend using static (x,y,R,G,B) for 5D graph and animated (x,y,t,R,G,B) for 6D. Use it wisely!
In the example I show all approaches mentioned above. i chose gravity field and calculate the plane 0.25 units below the centre of gravity.
Assume a force field defined in polar coordinates as F=-r/r^3; F=1/r^2.
Here both x and yare in range of -1;1 and same size N.
F is the MxMx3 matrix where F(ii,jj) is force vector corresponding to x(ii) and y(jj).
Matrix H(ii,jj) is the norm of F(ii,jj) and X, Y and Z are matrices of coordinates.
Last command ensures that F values are in (-1;1) range. The F./2+0.5 moves values of F so they fit into RGB range. The colour meaning will be:
black for (-1,-1,-1),
red for (1,-1,-1),
grey for (0,0,0)
Un-comment the type of plot You want to see. For quiver use resolution of 0.1, for other cases use 0.01.
clear all,close all
% Definition of coordinates
resolution=0.1;
x=-1:resolution:1;
y=x;
z=-.25;
%definition of matrices
F=zeros([max(size(x))*[1 1],3]); % matrix of the force
X=zeros(max(size(x))*[1 1]); % X coordinates for quiver3
Y=X; % Y coordinates for quiver3
Z=X+z; % Z coordinates for quiver3
% Force F in polar coordinates
% F=-1/r^2
% spherical -> cartesian transformation
for ii=1:max(size(x))
for jj=1:max(size(y))
% temporary variables for transformations
xyz=sqrt(x(ii)^2+y(jj)^2+z^2);
xy= sqrt(x(ii)^2+y(jj)^2);
sinarc=sin(acos(z/xyz));
%filling the quiver3 matrices
X(ii,jj)=x(ii);
Y(ii,jj)=y(jj);
F(ii,jj,3)=-z/xyz^2;
if xy~=0 % 0/0 error for x=y=0
F(ii,jj,2)=-y(jj)/xyz/xy*sinarc;
F(ii,jj,1)=-x(ii)/xyz/xy*sinarc;
end
H(ii,jj)=sqrt(F(ii,jj,1)^2+F(ii,jj,2)^2+F(ii,jj,3)^2);
end
end
F=F./max(max(max(F)));
% quiver3(X,Y,Z,F(:,:,1),F(:,:,2),F(:,:,3));
% image(x,y,F./2+0.5),set(gca,'ydir','normal');
% surf(x,y,Z,F./2+.5,'linestyle','none')
% surf(x,y,H,'linestyle','none')
surfc(x,y,H,'linestyle','none')
% contour3(x,y,H,15)
I would like to find the two (2) intersection points when a linear line goes through a circle's centrum (x,y).
r = 13 radius
x = 0 x-coordinate
y = 7 y-coordinate
k = 9 slope value(?) y=kx+m y=9x+m
So first I'm drawing a circle with r=13 and a centrum of (0,7).
r=13
x=0
y=7
k=9
hold on
z = 0:pi/50:2*pi;
xunit = r * cos(z) + x;
yunit = r * sin(z) + y;
plot(xunit, yunit);
I'm wondering if it would be possible to plot up a circle in an easier way? Something like
(x−cx)^2 + (y −cy)^2 = r^2
(x-0)^2 + (y-7) = 13^2
I've tried this
plot((x−cx)^2 + (y −cy)^2 = r^2)
It doesn't do anything at all so the code must be incorrect.
Well, then I'm drawing the linear equation by calculating
y=kx+m
k=9
the line goes thorugh (0,7)
7=9*0+m
m=7
y=9x+7
so since I'm new to MatLab it took me a while to actually draw the line. I didn't find any easy function to plot it so I plotted a line like this:
I took some random values for x or y and calculated some coordinates.
(0,7)
(2,25)
(-2,-11)
plot([-2,2],[-11,25])
Result image: http://i.imgur.com/ag6HJlm.jpg
So now I just need to solve the intersection points with "solve" function. So well I would really appreciate some help!
best regards
Here is one approach to it:
%Place your lines and figures on the grid
linexypos = eye(100);
shapexypos = flipud(eye(100)) ;
% Guess where they come together
intersection = filter2(ones(3),linexypos + shapexypos);
[quality, loc] = max(intersection(:))
Note that you have to guess, as two lines with widths of 1 pixel may not have exactly the same location. (consider [1 0; 0 1] and [0 1;1 0], they cross but never exactly overlap).
If you want to visualize the situation, try contour(intersection)
I'm looking to create a "web" between a set of points where the data tells whether there is a link between any two points.
The way I thought of would be by plotting every couple points, and overlaying each couple on top of eachother.
However, if there is a way to just simple draw a line between two points that would be much easier.
Any help would be appreciated!
If you can organize the x and y coordinates of your line segments into 2-by-N arrays, you can use the function PLOT to plot each column of the matrices as a line. Here's a simple example to draw the four lines of a unit square:
x = [0 1 1 0; ...
1 1 0 0];
y = [0 0 1 1; ...
0 1 1 0];
plot(x,y);
This will plot each line in a different color. To plot all of the lines as black, do this:
plot(x,y,'k');
Use plot. Suppose your two points are a = [x1 y1] and b = [x2 y2], then:
plot([x1 x2],[y1 y2]);
If you meant by I'm looking to create a "web" between a set of points where the data tells whether there is a link between any two points actually some kind of graph represented by its adjacency matrix (opposite to other answers simple means to connect points), then:
this gplot function may indeed be the proper tool for you. It's the basic visualization tool to plot nodes and links of a graph represented as a adjacency matrix.
use this function:
function [] = drawline(p1, p2 ,color)
%enter code here
theta = atan2( p2(2) - p1(2), p2(1) - p1(1));
r = sqrt( (p2(1) - p1(1))^2 + (p2(2) - p1(2))^2);
line = 0:0.01: r;
x = p1(1) + line*cos(theta);
y = p1(2) + line*sin(theta);
plot(x, y , color)
call it like:
drawline([fx(i) fy(i)] ,[y(i,1) y(i,2)],'red')
Credit: http://www.mathworks.com/matlabcentral/answers/108652-draw-lines-between-points#answer_139175
Lets say you want a line with coordinates (x1,y1) and (x2,y2). Then you make a vector with the x and y coordinates: x = [x1 x2] and y=[y1 y2].
Matlab has a function called 'Line', this is used in this way:
line(x,y)
If you want to see the effect of drawing lines, you can use plot inside for loop note that data is a n*2 matrix containing the 'x,y' of 'n' points
clf(figure(3))
for i = 1 : length(data)-1
plot([data(i,1),data(i+1,1)], [data(i,2),data(i+1,2)], '-*');
hold on
end
hold off
Or can use this statement to draw it in one step
plot(data(:,1), data(:,2), '-*');