How to plot a polar plot? - matlab

the question
Given N=1, an antenna has a radiation pattern given as y(theta)=sin(N4pitheta)/(N4pitheta) Assume that the formula is valid over the range of . Generate a polar plot of the radiation pattern. Use 400 points for your graph.
here is my code, I keep getting "THETA and RHO must be the same size." what does this mean and how can I fix my cod? thanks
function [graph,x,y]=question3(N)
x=linspace(-pi,pi,400);
y=(sin(N*4*pi*x)/(N*4*pi*x));
graph='polar(x,y)';
end

Your problem is that you are performing matrix division, not elementwise division.
change y=(sin(N*4*pi*x)/(N*4*pi*x)) to y=(sin(N*4*pi*x)./(N*4*pi*x))
Note that in Matlab, * and / will perform matrix operations while .* and ./ will perform array operations.
Read more here.
Side note: It looks like you are using eval to evaluate the output of that function. If it is your profesor who did this, leave it, but remember that even Matlab staff themselves suggest never to use eval.

Related

How to plot a antenna pattern in MATLAB given by a normalized field pattern?

I am trying to use MATLAB to create a polar plot of an antenna pattern given a normalized field pattern (remember 0<𝜃<180°). The antenna pattern is in this image:
I was trying to create a function to plot it but when I call the function I created, I get an error. Can anyone help?
My current code I have so far is in this image:
You can do this in a vectorized manner using element-wise operations instead of a loop.
Note that, although the variables are defined as symbolic in your code, they are then changed to double when you initiallize them with zeros.
The mathematical function is undefined at theta= pi. Its limit is 0, but if you compute it directly with double precision you get the value -1.5, produced by numerical inaccuracies inherent to floating-point data types. The best way to avoid this issue is to use an odd number of samples, so that theta is never too close to pi.
Lastly, the polar function is not recommended. It's better to use polarplot.
N = 101; % odd number of samples
theta = (0:N-1)/N*2*pi;
f = cos(1.5*pi*cos(theta))./sin(theta); % vectorized. Note element-wise division
polar(theta, f)

Using matlab to obtain the vector fields and the angles made by the vector field on a closed curve?

Here is the given system I want to plot and obtain the vector field and the angles they make with the x axis. I want to find the index of a closed curve.
I know how to do this theoretically by choosing convenient points and see how the vector looks like at that point. Also I can always use
to compute the angles. However I am having trouble trying to code it. Please don't mark me down if the question is unclear. I am asking it the way I understand it. I am new to matlab. Can someone point me in the right direction please?
This is a pretty hard challenge for someone new to matlab, I would recommend taking on some smaller challenges first to get you used to matlab's conventions.
That said, Matlab is all about numerical solutions so, unless you want to go down the symbolic maths route (and in that case I would probably opt for Mathematica instead), your first task is to decide on the limits and granularity of your simulated space, then define them so you can apply your system of equations to it.
There are lots of ways of doing this - some more efficient - but for ease of understanding I propose this:
Define the axes individually first
xpts = -10:0.1:10;
ypts = -10:0.1:10;
tpts = 0:0.01:10;
The a:b:c syntax gives you the lower limit (a), the upper limit (c) and the spacing (b), so you'll get 201 points for the x. You could use the linspace notation if that suits you better, look it up by typing doc linspace into the matlab console.
Now you can create a grid of your coordinate points. You actually end up with three 3d matrices, one holding the x-coords of your space and the others holding the y and t. They look redundant, but it's worth it because you can use matrix operations on them.
[XX, YY, TT] = meshgrid(xpts, ypts, tpts);
From here on you can perform whatever operations you like on those matrices. So to compute x^2.y you could do
x2y = XX.^2 .* YY;
remembering that you'll get a 3d matrix out of it and all the slices in the third dimension (corresponding to t) will be the same.
Some notes
Matlab has a good builtin help system. You can type 'help functionname' to get a quick reminder in the console or 'doc functionname' to open the help browser for details and examples. They really are very good, they'll help enormously.
I used XX and YY because that's just my preference, but I avoid single-letter variable names as a general rule. You don't have to.
Matrix multiplication is the default so if you try to do XX*YY you won't get the answer you expect! To do element-wise multiplication use the .* operator instead. This will do a11 = b11*c11, a12 = b12*c12, ...
To raise each element of the matrix to a given power use .^rather than ^ for similar reasons. Likewise division.
You have to make sure your matrices are the correct size for your operations. To do elementwise operations on matrices they have to be the same size. To do matrix operations they have to follow the matrix rules on sizing, as will the output. You will find the size() function handy for debugging.
Plotting vector fields can be done with quiver. To plot the components separately you have more options: surf, contour and others. Look up the help docs and they will link to similar types. The plot family are mainly about lines so they aren't much help for fields without creative use of the markers, colours and alpha.
To plot the curve, or any other contour, you don't have to test the values of a matrix - it won't work well anyway because of the granularity - you can use the contour plot with specific contour values.
Solving systems of dynamic equations is completely possible, but you will be doing a numeric simulation and your results will again be subject to the granularity of your grid. If you have closed form solutions, like your phi expression, they may be easier to work with conceptually but harder to get working in matlab.
This kind of problem is tractable in matlab but it involves some non-basic uses which are pretty hard to follow until you've got your head round Matlab's syntax. I would advise to start with a 2d grid instead
[XX, YY] = meshgrid(xpts, ypts);
and compute some functions of that like x^2.y or x^2 - y^2. Get used to plotting them using quiver or plotting the coordinates separately in intensity maps or surfaces.

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.

Cepstrum deconvolution Matlab code

I have a little code, that should implement cepstrum deconvolution for minimum phase FIR filter design, but being nonmatlab guy I'm struggling with understanding it. Can someone help?
wn = [ones(1,m)
2*ones((n+odd)/2-1,m)
ones(1-rem(n,2),m)
zeros((n+od d)/2-1,m)];
y = real(ifft(exp(fft(wn.*real(ifft(log(abs(fft(x)))))))));
Mainly I don't understand the first line, ".*" symbol in second line and also probably at some point there should be conversion from real to complex domain in the second line, but I have no idea where. Any ideas?
In the first line you are constructing the matrix wn row by row.
.* operator means element-wise multiplication. * alone would mean matrix multiplication.
In fact you should pay attention to the size of x and wn which must be the same for the element-wise multiplication to have sense.
actually there isn't any conversion from real to complex in the second line. There are the functions log, fft, ifft that may return complex values depending on the input.
You can access the Matlab help by the commands help or doc (for example doc ones should produce the documentation of the ones function - this produce a matrix filled with ones of the size specified by it's arguments).
To quickly summon the help when you're inspecting some code you can use Matlab's inline help by pressing the button F1 when the cursor is at the end of a function name (just before the parenthesis).

I can't multiple numbers in matlab

I need to find rp for each x but I'm having a hard time because matlab gives me this error
Error in enee_408e_hw2_pb1 (line 6)
rp=(z2*cos(t2)-z1*cos(x))/(z2*cos(t2)+z1*cos(x))
here is my code
x=0:.01:pi/2;
n2=1.7;
t2=asin(sin(x)/n2);
z1=377*cos(x);
z2=377\cos(x);
rp=(z2*cos(t2)-z1*cos(x))/(z2*cos(t2)+z1*cos(x));
I want to calculate rp for each angle from 0 to pi/2 and then plot it. What am I doing wrong?
Every operation is perform on same cell in z1 and z2. Matlab/Ocatave calculate enquation based on variable's type. So if you put * between vectors it try to calculate cross product. Probably problem was \, I have no idea what Matlab tried to do with it. Adding . before operator change approach to more natural (for non mathematical folks) and multiply corresponding cells.
But still I'm not sure it this is what you expected
rp=(z2.*cos(t2)-z1.*cos(x))./(z2.*cos(t2)+z1.*cos(x));
First, why are you using \ in this line?
z2=377\cos(x);
There is a difference between \ and / in MATLAB.
It is time to learn about the difference between ./ and .* versus / and * in MATLAB.
When you want element-wise operations, use the operators with a dot.