I have used MATLAB to draw the following gain function of an antenna array, the code was as follows,
Nt=8;
deltat=1;
Lt=8;
omegat=-2:0.01:2;
for j=1:length(omegat)
gainfunction(j)= (1/Nt) * exp(i*pi*deltat* omegat(j)* (Nt-1)) * (sin(pi*Lt*omegat(j))/sin(pi*Lt*omegat(j)*Nt^-1));
end
plot(omegat,abs(gainfunction))
title( 'Radiation Pattern Cartesian Plot','linewidth',30)
grid on
ylabel('|f(\Omega_r)|','linewidth',25)
xlabel('\Omega_r','linewidth',15)
The image below is a radiation pattern of an antenna, i.e it shows the gain function denoted by |f(\Omega)| as function of $\Omega$ from -2 to 2.
My question is I would like to plot the following in polar coordinates to see how the main lobe is in degrees.
Any thoughts on how I can continue to the polar plot using MATLAB ?
polar (omegat*pi/2, abs(gainfunction));
I scaled omegat by pi/2 because I'm not sure what your convention here is. I'm assuming omegat must range from -pi to +pi and hence, I've scaled it by pi/2. Modify the scaling as you see fit.
Also, don't use i or j as loop counters in matlab. They're used in many places as iota or the sq root of minus one.
Related
I want to morph sphere, using the following equation:
R=1+k*(cos(4*elev)+sin(4*az)),
Spherical coordinates elev and az are transferred as gl_Vertex. The problem is to calculate normals for such morphing. I may calculate them, just shifting az and elev a bit, the obtain 2 more "virtual" vertices and use standard approach with cross-product, but it looks rather ugly and expensive approach.
Is their are any way to calculate normals also parametrically for such kind of morphing?
UPDATE:
Thanks to #meowgoesthedog, but I still have problems. My implementation of this formula (code below) did not work:
Am i correct and it should be:
cos(theta)*(k*(cos(4*theta)+sin(4*phi))+1) or cos(theta*(k*(cos(4*theta)+sin(4*phi))+1))?
Are normals calculated in cartesian coordinate system?
My code in Matlab:
close all
clear all
N=100;
phi_range=linspace(-pi,pi,N+2);
theta_range=linspace(-pi/2,pi/2,N);
phi_range([1,end])=[];
k=6;
% generate morphed sphere
PHI=repmat(phi_range,N,1);
THETA=repmat(theta_range',1,N);
R=1+k*cos(4*THETA)+k*sin(4*PHI);
% convert to cartesian coordinates
[X,Y,Z]=sph2cart(PHI,THETA,R);
%% meowgoesthedog formula
S=k.*(cos(4.*THETA)+sin(4.*PHI))+1;
V1_x = cos(PHI).*(cos(THETA).*S-4.*k.*sin(THETA).*sin(4.*THETA));
V1_y = sin(PHI).*(cos(THETA).*S-4.*k.*sin(THETA).*sin(4.*THETA));
V1_z = -sin(THETA).*S-4.*k.*sin(4.*THETA).*cos(THETA);
V2_x = sin(THETA).*(4.*k.*cos(PHI).*cos(4.*PHI)-sin(PHI).*S);
V2_y = sin(THETA).*(4.*k.*sin(PHI).*cos(4.*PHI)+cos(PHI).*S);
V2_z = 4.*k.*cos(THETA).*cos(4.*THETA);
V1=cat(3,V1_x,V1_y,V1_z);
V2=cat(3,V2_x,V2_y,V2_z);
Normals=cross(V1,V2);
% normalize
Normals=Normals./sqrt(sum(Normals.^2,3));
%% plot and compare results:
hold all
surfnorm(X,Y,Z,'EdgeAlpha',0.5)
quiver3(X,Y,Z,Normals(:,:,1),Normals(:,:,2),Normals(:,:,3),'m')
On figures below red - correct normals, magenta which I calculate.
We can compute an analytical expression for the normal using differential geometry. Let's first state the form of the Cartesian parametric coordinate:
Locally at any point on the surface, there is a 2D coordinate system spanned by the unit vectors in the directions of increasing θ and φ.
These vectors are given by:
The normal is simply given by the cross-product of these two vectors (un-normalized):
After some very tedious algebra we obtain:
(The formula becomes too long to legibly display past this point, and probably not as efficient to evaluate either.)
EDIT
It appears that I have used the conventional definition of θ (angle from the +Z axis) instead of Matlab's elev. Redefining the equations this would give:
Where ψ = ½π - θ is the elevation.
I am trying to write a MATLAB script to give me a contour map. The contour map must be created from inputs that I generated from 100 images.
The story is like this:
I have 100 images on which I ran an image processing algorithm for optimization. Now, I got their energy curves. So, I have 100 energy curves. I want to create a contour map that will show me where the points are denser on the plot. (the energy curves are plotted as energy vs. iteration with fixed number of iterations)
The following is my variable:
energy(iteration,numImages)
Hope I explained it well.
Thanks in advance.
I interpret your question to boil down to how can I create a surface plot with colors according to the energy found in energy. I would solve this by using the contour function with a grid generated using meshgrid. If each image is described in 1000 data points with 100 files the plot can be generated as follows:
% using stuff as random junk instead of energy
numPoints = 1000;
numFiles = 100;
stuff = rand(1000,100); % replace with actual information
[X, Y] = meshgrid(1:numFiles, 1:numPoints);
contour(X,Y,stuff);
You can also create a 3D surface plot using surf and the same logic.
From what i see of you graph (and using the comments also), one possible way is to use plot3 to plot a line in 3D for every plot.
For doing so, you can use something like this code:
x=(0:0.01:1)';
aexp=zeros(100,numel(x));
hold on
for ii=1:100;
% aexp(ii,:)=exp((-x+ii/10)); %exponential
aexp(ii,:)=exp(-(x-ii/100).^2); %~gaussian
% aexp(ii,:)= x*ii; %linear increase
plot3(x,aexp(ii,:),ii*ones(1,numel(x)));
end
% set(gca,'yscale','log'); % uncomment if you need logscale.
giving
I have a few options of plot. It always plot from the XY view. I changed by hand, but you can use the view command. Notice that i used a simple counter to make the spacing in the z direction.
In a similar manner, you can plot using the contour. For my code, after the data have been generated in the for loop, remove/comment the plot3 and add:
contour(aexp) %outside the for loop,
giving
Notice that i have not really take care what i'm plotting. You can find more info on contour in the Matlab page .
You commented that the x-axis should be number of iterations, y-axis should be energy and z-axis should be the information containing how many lines are passing through from some areas. For this, make a qq variable, being it qq=number_of_lines(number of iterations,energy) . Make a discrete grid for the energy if you don't have one. Number of iterations is probably discrete anyway. The function is you who need to devise, but i would go for something which checks the number of lines for every energy and every iteration. In this case you will have the z-function that depends on y and x, that is the case to use contour or surface.
My function above make a line for every ii point, to have a 3d function. An edition for another extra loop is not hard. Just remember to have the same regular grid for every point, otherwise you will have trouble.
I want to plot an inequality in 3d using surf. My condition is
0<=x<=1
0<=y<=1
0<=z<=x/(1+y)
I can create a surface plot using the following commands
[x y]=meshgrid(0:0.01:1);
z=x./(1+y);
surf(x,y,z);
This plot gives me regions where z=x/(1+y) but I am interested in regions where 0<=z<=x/(1+y) over all values of x and y. However, I am unable to plot/color the region explicitly. Can you please help.
A similar question has been asked but there was no acceptable answer and my question is also different.
Using isosurface you can show the boundary. There are two options, first create the points
[X,Y,Z]=meshgrid(0:.01:1);
then plot the boundaries in the z-direction (i.e. Z=0 and Z=X./(1+Y))
isosurface(X,Y,Z,Z.*(X./(1+Y)-Z),0)
or plot all the boundaries (including X=0, X=1, Y=0 and Y=1)
isosurface(X,Y,Z,Z.*(X./(1+Y)-Z).*X.*(X-1).*Y.*(Y-1),0)
All you have to do is come up with a function that is constant on any boundary, its value inside or outside is irrelevant as long as it is not zero.
I am looking for help for my particular problem.
I have a contour plot created from XYZ data. This plot contains 2 broad peaks with one more intense than the other.
When the most intense peak is aligned with the Y axis, I can perform a fitting of every YZ curve at each X values. I usually do a gaussian fit to plot the peak center on the same graph.
In some cases I need to perform the same fitting but no along the Y axis direction (in this case I just plot YZ scan at every different X values) but along another arbitrary direction.
For the moment the only way I found is the following:
-plot the contour plot and find for the position of the most intense peak
-if the position is not aligned with the Y axis, then rotate all the datas and plot again the contour
-perform the YZ gaussian fit for every X value
- Rotate the resulting XY position to go back to the original plot
-plot the XY position as a line on the original contour plot
this is quite long and requires a lot of memory. i would like ot know if there is a more elegant/faster way.
Thanks for your help
David
I take it you want to extract data from the (x,y,z) data along some arbitrary line in order to make a fit. A contour plot will show only part of the data, the full z(x,y) data can be shown with imagesc etc. Say you want the data along line defined by two points (x1,y1) -> (x2,y2). According to the eq of the line, the line y=a*x+b the slope a is (y2-y1)/(x2-x1) and b=y1-a*x1. For example, I'll select (x,y) coordinates in the following contour:
Create data and end points:
m=peaks(100);
x1=11 ; x2=97;
y1=66; y2=40;
Thus the line parameters are:
a=(y2-y1)/(x2-x1);
b=y1-a*x1;
and the line is:
x=x1:x2;
y=round(a*x+b);
select the proper (x,y) elements using linear indexing:
ind=sub2ind(size(m),y,x)
plot:
subplot(2,1,1)
contour(m,10); hold on
line([x1 x2],[y1 y2],'Color',[1 0 0]);
subplot(2,1,2)
plot(m(ind))
You can now use vec=m(ind) to fit your function.
Suppose you have f(x)=x-floor(x).
By this, you can generate the grooves by gluing the top side and the
bottom side together and then squeezing the left to zero -- now you
have a conical helix: the line spins around the cone until it hits the
bottom. You already have one form of the equations for the conical
helix namely x=a*cos(a); y=a*sin(a); z=a. Now like
here:
How can you project the conical helix on the cone in Matlab?
I'd approach your problem without using plot3, instead I'd use meshgrid and sinc. Note that sinc is a matlab built in functions that just do sin(x)./x, for example:
So in 1-D, if I understand you correctly you want to "project" sinc(x) on sqrt(x.^2). The problem with your question is that you mention projection with the dot product, but a dot product reduces the dimensionality, so a dot product of two vectors gives a scalar, and of two 2D surfaces - a vector, so I don't understand what you mean. From the 2-D plot you added I interpreted the question as to "dress" one function with the other, like in addition...
Here's the implementation:
N=64;
[x y]=meshgrid(linspace(-3*pi,3*pi,N),linspace(-3*pi,3*pi,N));
t=sqrt(x.^2+y.^2);
f=t+2*sinc(t);
subplot(1,2,1)
mesh(x,y,f) ; axis vis3d
subplot(1,2,2)
mesh(x,y,f)
view(0,0) ; axis square
colormap bone
The factor 2 in the sinc was placed for better visualization of the fluctuations of the sinc.