I am beginner in Matlab, this is my code so far to plot function
A=[5:5:60];
alpha=[0:1:90].*(pi/180);
omega=[24];
for k = 1:12
A1=A(k);
B=[0:A1./5: A1];
for j = 1:6
for i = 1:91
I(k,j,i)=A(k)+B(j)*sin(2*alpha(i)+omega)*sin(2*alpha(i)+omega);
end
end
end
figure(1), plot(alpha, squeeze(squeeze(I(6,3,:))))
How do I plot relation between A and b
from each I at a set A and B.
and to use max and min functions
to get Imax and Imin.
And then I want to calculate PI, then plot PI vs (A,B) on a 3d plot.
can any one help?
Related
My name is Alex and I am new on programming on Matlab. I am writing a code to plot the contour of an electric field on a x-y axis. I am using pcolor(x,y,real(E)) in order to do that. Then, I use shading interp to show the different colors of my electric field on a plane x-y.
First of all, I have a vector with some values R that represent radii of circles. Then, I transform the vector r (cylindrical coordinates) into x and y vectors (Cartesian coordinates) with the code
This is the code I used to create the image
r=(125/2)*1e-6;
a0=(105/2)*1e-6;
a=(81/2)*1e-9;
z=2*1e-3;
theta=(pi*19)/180;
lambda=632*1e-9;
n1=1.445;
n2=1.433;
delta = sqrt(n1^2 - n2^2);
Kco=n1*2*pi/lambda;
V=(2*pi*delta*a0)/lambda;
z=linspace(0,z,500);
g=zeros(1,length(z));
r=linspace(r,a,length(z));
B = zeros(1,length(r));
psi = zeros(length(r),length(r));
E = zeros(1,length(r));
M=length(z)
g = (a0)./(a+z*theta);
R=(g')*r;
beta = linspace(0,2*pi,length(z));
for i=1:length(z)
w = R(i);
w = repmat(w,[length(z) 1]);
[X Y] = pol2cart(beta,w');
x(i,:) = X;
y(i,:)=Y;
clear w;
clear X Y;
end
for i=1:length(r)
if r(i)<=a0
n1=1.445;
n2=1;
delta = sqrt(n1^2 - n2^2);
end
R02(i)=(a0^2)./(((delta*2*pi*(a+z(i)*theta)/lambda)-1));
psi(i,:)=exp(-(x(i,:).^2+y(i,:).^2)./(2*R02(i))) ;
end
clear R02
B=sqrt(Kco^2 -((2*V).*g)./(a0^2)+(g.^2)./(a0^2));
for i=1:length(r)
E(i,:) = sqrt(x(i,:).^2+y(i,:).^2).*psi(i,:).*(exp(-1i*(B(i)).*z(i)));
end
pcolor(x,y,real(E));
shading interp
z in this part is the z-axis and this vector has some values too.
So the electric field is a function of x,y and z after the transformation for cartesian coordinates. Now, I am ready to plot the graph and I get
But it should have more colors than the image has shown there. All I have done is simulation. There is no data from experiments. But it does not shows what the experiment shows. Any help is very appreciated. Thank you very much in advance.
I would like to obtain the level curves of a given function z=f(x,y) without using the countours function in the Matlab environment.
By letting Z equal to some constant 'c' we get a single level curve. I would like to obtain an expression of the resulting function of the form y=f(x) to be able to study other properties of it.
Basic: Example 1: Easy game
Let's consider the problem of plotting level curves of z=-x^2-y^2+100 for x,y:-10;10 and z=1.
[X,Y] = meshgrid(-10:.1:10);
Z = -X.^2+-(Y).^2+100;
surf(X,Y,Z)
we obtain the following figure:
The countour function gives me what I was looking for:
h=[1,1]
contour(X,Y,Z,h)
I can get the same result by solving the equation with respect to x
syms x y;
soly = solve(1==-x.^2-y^2+100, y)
t=soly(1)
x=-10:0.1:10;
figure;
plot(x,(99-x.^2).^(1/2));
hold on
plot(x,-(99-x.^2).^(1/2));
soly =
(99 - x^2)^(1/2)
-(99 - x^2)^(1/2)
The Problem: Example 2
Let's consider the following 3D function.
This function is the sum of guassians with random centers, variances and peaks. You can get it with the following code:
%% Random Radial Basis functions in space
disp('3D case with random path - 10 rbf:');
N = 10 % number of random functions
stepMesh = 0.1;
Z = zeros((XMax-XMin)/stepMesh+1,(XMax-XMin)/stepMesh+1);
[X,Y] = meshgrid(XMin:stepMesh:XMax);
% random variance in [a;b] = [0.3;1.5]
variances = 0.3 + (1.5-0.3).*rand(N,1);
% random amplitude [0.1;1]
amplitudes = 0.1 + (1-0.1).*rand(N,1);
% Random Xcenters in [-XMin;xMax]
Xcenters = XMin+ (XMax-XMin).*rand(N,1);
Ycenters = YMin+ (YMax-YMin).*rand(N,1);
esp=zeros(N,1);
esp=1./(2*(variances).^2);
for i=1:1:N
disp('step:')
disp(i)
Xci=Xcenters(i,1)*ones((XMax-XMin)/stepMesh+1,((XMax-XMin)/stepMesh+1)*2);
Yci=Ycenters(i,1)*ones((YMax-YMin)/stepMesh+1,((YMax-YMin)/stepMesh+1)*2);
disp('Radial Basis Function: [amplitude,variance,center]');
disp(amplitudes(i,1))
disp(variances(i,1))
disp(Xcenters(i,1))
disp(Ycenters(i,1))
Z = Z + 1*exp(-((X-Xci(:,1:((XMax-XMin)/stepMesh+1))).^2+(Y-Yci(:,((XMax-XMin)/stepMesh+2):((YMax-YMin)/stepMesh+1)*2)).^2)*esp(i,1).^2);
end
surf(X,Y,Z)
The countour3 function draws a contour plot of matrix Z in a 3-D view, which is really nice, but it doesn't give me the expression of these functions in 2D.
figure;
contour3(X,Y,Z);
grid on
box on
view([130,30])
xlabel('x-axis')
ylabel('y-axis')
zlabel('z-axis')
The Question:
How can I get an explicit expression of the level curves of the second example?
I am working on a joint pdf problem in which the random variable
U = sqrt(X^2+Y^2)
X and Y are uniformly distributed over (-2,2). I want to plot joint pdf of X and Y. Then compute pdf of U and plot it as well. I am using matlab R2011a, and so far, I have come up with the following code. On running the code I got an error message
Undefined function or method 'makedist' for input arguement type 'char'.
I found out that makedist is not on 2011 version. So I tried using
a=-2;
b=2;
X=a+(b-a)*rand(-10,10);
Y= a+(b-a)*rand(-10,10).
However, I am not sure how to compute pdfs of X and Y, and then joint pdf of XY from this. Any help, partial or holistic, is appreciated.
Here is the matlab code for the problem
%% Create distribution objects for X~U(-2,2) and Y~U(-2,2)
pdx=makedist('Uniform','lower',-2,'upper',2);
pdy=makedist('Uniform','lower',-2,'upper',2);
%Compute the pfs
x_ref=-10:1:10;
y_ref=-10:1:10;
pdf_x=pdf(pdx,x_ref);
pdf_y=pdf(pdy,y_ref);
% Plot the pdfs
figure 1;
stairs(x_ref,pdf_x,'g','Linewidth',2);
hold on;
stairs(y_ref,pdf_y,'r','Linewidth',2);
ylim([0 1.5]);
hold off;
% Joint pdf of x and Y
pdfXY=pdf_x*pdf_y;
figure 2;
plot(pdfXY);
%CDF and PDF of U
U=sqrt(X^2+Y^2);
Umin=0;
Umax=sqrt(b^2+b^2);
a=lower;
b=upper;
x=sqrt(U^2-Y^2);
xmin=0;
xmax=x;
ymin=0;
ymax=U;
Ucdf=integral2(pdfXY,xmin,xmax,ymin,ymax);
% plot CDF of U
figure 3;
plot(Ucdf)
I am just looking to plot the regions than for any specific sample set. X and Y are continuous independent uniform random variables.
As your x and y are independent at random, the theoretical joint distribution is just a product of the two
P(x,y) = P(x)*P(y)
In terms of MATLAB code, you may think of x and y running along two different dimensions:
N = 10; %// think of a probability mass function over N points
x = linspace(-2,2, N);
y = linspace(-2,2, N)';
Px = ones(N,1)./N;
Py = ones(1,10)./N;
%// Then the joint will be:
Jxy = bsxfun(#times, Px , Py);
figure
pcolor(x,y,Jxy)
You can now plug whatever distribution you like, if they are independent for Px and Py, and it will work
I am trying to draw a curve where each points are specific distance away from each other.
Now, blow shows pretty much what I wanted to do but I want sin like curve and not constant radius.
R = 50; %radius
Gap = 0.1; % gap between points
Curve = 180;
rad = 0;
n= pi*2*R*Curve/360/Gap; % n is length of arc
th = linspace( pi, rad ,n);
x = R*cos(th)+R;
y = R*sin(th)+100;
PathDB.Route1.x(1:1001,1)=0;
PathDB.Route1.y = (0:Gap:100)';
LengthY = length(PathDB.Route1.y);
PathDB.Route1.x(1001:1001+length(x)-1,1)=x ;
PathDB.Route1.y(LengthY:LengthY+length(y)-1) = y;
LengthX = length(PathDB.Route1.x);
LengthY = length(PathDB.Route1.y);
PathDB.Route1.x(LengthX:LengthX+1000,1)=PathDB.Route1.x(LengthX,1);
PathDB.Route1.y(LengthY:LengthY+1000,1)= (PathDB.Route1.y(LengthY,1):-Gap:0);
plot(PathDB.Route1.x, PathDB.Route1.y);
grid ;
axis equal
All I want to do is instead of perfect curve, I want to add sin like curve which are plotted by 0.1.
I'm sorry about my poor coding skills I hope you can understand and help me.
Any advice is appreciated!
Rui
If you want this to be robust, you'll probably need to use the Matlab Symbolic Toolbox. If you take a function f, and want to plot equidistant points along it's curve between a and b, you could do something like this (using sin(x) as the example function):
sym x;
f = sin(x);
g = diff(f); %derivative of f
b=5;
a=0;
%check syntax on this part
arc_length = int(sqrt(1+g^2)); %create function for arc_length
Sc = arc_length(b)-arc_length(a); %calculate total arc length
n = 30 %number of points to plot (not including b)
Sdist = Sc/n; %distance between points
xvals = zeros(1,(n+1)); %initialize array of x values, n+1 to include point at b
Next, we will want to iterate through the intervals to find equidistant points. To do this we take each S_ij where i represents the x value of the low end of the interval and j represents the high end. We start with i=a and stop when i=b. Using the inverse of our arc length equation we can solve for j. Sdist == S_ij => Sdist = arc_length(j) - arc_length(i) => Sdist+arc_length(i)=arc_length(j). Using this, we can compute: j = arc_length_inverse(Sdist+arc_length(i)). One way of implementing this would be as follows:
arc_length_inv = finverse(arc_length);
i=a
xvals(1)=i;
for ii=1:n
j = arc_length_inv(Sdist+arc_length(i)); %calculate high end
i = j %make high end new low end
xvals(ii+1)=i; %put new low end value in x array
end
With this array of x values, you can compute yvals=sin(xvals) and plot(xvals,yvals)
Some of this code probably needs tweaking but the math should be sound (the formula for arc length came from http://en.wikipedia.org/wiki/Arc_length). Mathematically, this should work for any function integrable on [a,b] and whose arc_length function has an inverse; that said, I do not know the capabilities of the int() and finverse() functions.
If you do not need robustness you can follow these steps for your specific function, doing the math by hand and entering the functions manually;
I'm working on a project which includes use of accelerometer and gyroscope to get the orientation an object. I can pass the 3d orientation vector from arduino to matlab via serial communication.
I want to plot the vector in matlab to make real time analysis. I'm using quiver3d and drawnow functions in a loop to plot the vector but quiver3d function is very slow so I can see the orientation of object after like 20 seconds later.
Is there any way to plot 3d vectors faster?
Thanks.
quiver plot may be too much for plotting only one vector in 3-D. You can achieve a similar plot by using a simple plot3 such as the one plotted below.
In this plot, the origin of the vector is the blue dot, and the direction is given by the red line.
The code
%v is the direction of the vector (3 cartesian coordinates)
v = sort(randn(100,3));
v = bsxfun(#rdivide,v,sqrt(sum(v.^2,2)));
%xyz the origin of the vector
ind = linspace(-pi,pi,100);
x = cos(ind);
y = sin(ind);
z = ind;
%the plotting function
figure
for ii = 1:numel(ind)
plot3(x(ii),y(ii),z(ii),'bo'); %origin in blue
set(gca,'XLim', [-3 3], 'YLim', [-3 3], 'ZLim', [-3 3]);
hold on;
hl = plot3( linspace(x(ii), x(ii)+v(ii,1),10), ...
linspace(y(ii), y(ii)+v(ii,2),10), ...
linspace(z(ii), z(ii)+v(ii,3),10), ...
'r'); %direction in red
view(80,10);
pause(0.1);
%clf
end