Output of streamline in Matlab is empty - matlab

I want to use streamline to show a vector field. The vector field is singular in a point. I want to remove regions near the singularity (fo example regions which their distance to singularity is less than 1). I wrote below code but it doesn't show anything. Could anyone help me?
clear all;
close all;
r1 = 1; r2 = 5; % Radii of your circles
x_0 = 0; y_0 = 0; % Centre of circles
[x,y] = meshgrid(x_0-r2:0.2:x_0+r2,y_0-r2:0.2:y_0+r2); % meshgrid of points
idx = ((x-x_0).^2 + (y-y_0).^2 > r1^2 & (x-x_0).^2 + (y-y_0).^2 < r2^2);
x = sort(x(idx));
[x, index] = unique(x);
y = sort(y(idx));
[y, index] = unique(y);
U=cos(x)/sqrt(x.^2+y.^2);
V=sin(x)/sqrt(x.^2+y.^2);
streamslice(x,y,U,V);

The problem with your code is that U and V are all zeros, so you get white space. The reason for that is that you don't use elementwise division with ./. So as a first step you should write:
U = cos(x)./sqrt(x.^2+y.^2);
V = sin(x)./sqrt(x.^2+y.^2);
Now U and V are not zeros but are also not matrices anymore, so they are not a valid input for streamslice. The reason for that is that x and y are converted to vectors when calling:
x = sort(x(idx));
y = sort(y(idx));
My guess is that you can remove all this indexing, and simply write:
r1 = 1; r2 = 5; % Radii of your circles
x_0 = 0; y_0 = 0; % Centre of circles
[x,y] = meshgrid(x_0-r2:0.2:x_0+r2,y_0-r2:0.2:y_0+r2); % meshgrid of points
U = cos(x)./sqrt(x.^2+y.^2);
V = sin(x)./sqrt(x.^2+y.^2);
streamslice(x,y,U,V);
so you get:

I think you misunderstood the concept of streamslice. Is this you expecting?
close all;
r1 = 1; r2 = 5; % Radii of your circles
x_0 = 0; y_0 = 0; % Centre of circles
[xx,yy] = meshgrid(x_0-r2:0.2:x_0+r2,y_0-r2:0.2:y_0+r2); % meshgrid of points
% idx = ((xx-x_0).^2 + (yy-y_0).^2 > r1^2 & (xx-x_0).^2 + (yy-y_0).^2 < r2^2);
% x = sort(xx(idx));
% [x, index] = unique(x);
% y = sort(yy(idx));
% [y, index] = unique(y);
U=cos(xx)./sqrt(xx.^2+yy.^2);
V=sin(xx)./sqrt(xx.^2+yy.^2);
streamslice(xx,yy,U,V);

Related

Plot in a different colors for y-values above x-axis

Here I've plotted ((2*x.^2+3*exp(-x) -(100*a)).*sin(5*x))./(6*x.^2-9*x-42)-10 with input a=3, dx=0.1.
I would like to plot all positive y-values in blue and all negative y-values in red:
a = input('Please input a: ');
dx = input('Input step size dx: ');
if dx<0
fprintf('dx must be a non-negative number.');
while(dx<0)
dx = input('Input step size dx: ');
end
end
clf;
x = -10:dx:10;
y = ((2*x.^2+3*exp(-x) -(100*a)).*sin(5*x))./(6*x.^2-9*x-42)-10
plot(x,y)
ylim([-100,100])
To plot positive y-values in blue and negative y-values in red, I've tried initializing vectors for positive y-values and their domain, and the same vectors for negative y-values and their domain.
s = size(y);
x_1 = [];
x_2 = []; %negative
y_1 = [];
y_2 = []; %negative
for i = 1:s(2)
if(y(i) >0)
x_1 = [x_1,x(i)];
y_1 = [y_1,y(i)];
elseif (y(i) <0)
x_2 = [x_2,x(i)];
y_2 = [y_2,y(i)];
end
end
s_x1 = size(x_1);
s_x1_lim = s_x1(2);
s_x2 = size(x_2);
s_x2_lim = s_x2(2);
plot(x_1,y_1,'b');
xlim([x_1(1), s_x1_lim]);
hold on
plot(x_2,y_2,'r');
xlim([x_2(1), s_x2_lim]);
hold on;
xlim([-10,10])
ylim([-100,100]);
Problem is, this approach leaves some overlap which I would prefer not to have. How can I change this?
You can make two arrays: neg_x = x; neg_x (neg_x >0) = nan plot using 'r' and then the opposite for the positive values. This will, however, leave you with the pieces between the two arrays, i.e. your gaps. You can correct them by finding them and extending the index by one:
x = 0:0.1:6*pi;
y = sin(x);
neg_y = y; neg_y (neg_y>0) = nan;
tmp_y = isnan(neg_y);
idx = find(diff(tmp_y)==1); % find gaps
neg_y(idx+1) = y(idx+1); % correct gaps
idx = find(diff(tmp_y)==-1);
neg_y(idx) = y(idx);
pos_y = y; pos_y (pos_y<0) = nan;
plot(x, neg_y,'r');
hold on;
plot(x,pos_y,'b')
Using nan is a little help here, since MATLAB will automatically ignore those entries when plotting, i.e. leaving a nice gap, rather than a straight line.
Results in:

Would like to generate surface of revolution from bezier curve

I would like to generate surface of revolution from bezier curve. I have made bezier curve in MATLAB but beyond this point I am stuck and do not know how to proceed. Please help.
Below is the code that I have made.
clc
clear
close all
% Name : Savla Jinesh Shantilal
% Bits ID : 2021HT30609
%% Define inout parameters
B = [1,1; 2,3; 4,3; 3,1]; % Creating matrix for polygon vertices
[r,s] = size(B); % getting size of matrix in terms of rows and columns
n = r-1; % n+1 represents number of vertices of the polygon
np = 20; % represents number of equi-distance points on the bezier curve
t = linspace(0,1,np);
%% Plot polygon
for k = 1:n
plot([B(k,1),B(k+1,1)], [B(k,2),B(k+1,2)], 'r', 'LineWidth', 2)
hold on
grid on
end
%% Generate the points on the bezier curve
for j = 1:np
P = [0,0];
for i = 0:n
M(i+1) = (factorial(n)/(factorial(i)*factorial(n-i)))*((t(j))^i)*((1-t(j))^(n-i));
P = P + B(i+1,:)*M(i+1);
end
Q(j,:) = P;
end
%% Plot the bezier curve from the obtained points
for l = 1:np-1
plot([Q(l,1),Q(l+1,1)],[Q(l,2),Q(l+1,2)], '-- b', 'LineWidth', 2);
hold on
end
Usually one can use the built-in cylinder function for monotonically increasing x-values. Here, the bezier curve has non monotonic values from max(x) so we break it to two parts to parameterize it, and then add an angle rotation.
% first define the x and y coordinate from your Q info:
xx = Q(:,1);
yy = Q(:,2);
N = 1e2;
[~, id] = max(xx); % the position where we split
t = linspace(xx(1),xx(id),N);
% Parameterize the function:
t = linspace(0,2*(xx(id)-xx(1)),N);
x = zeros(1, N);
L = t <= ( xx(id)-xx(1) ); % the "Left" side of the curve
x(L) = t(L)+xx(1);
x(~L) = flip(x(L));
%define the r value
r = x;
r(L) = interp1(xx(1:id) ,yy(1:id) , x(L) ); % Left side
r(~L) = interp1(xx(id:end),yy(id:end), x(~L)); % right side (not left)
% define your x values
x = repmat(x', [1 N]);
% define the theta that will perform the rotation
theta = linspace(0,2*pi, N);
% initialize values for y and z
y = zeros(N);
z = zeros(N);
% calculate the y and z values
for i=1:N
y(i,:) = r(i) *cos(theta);
z(i,:) = r(i) *sin(theta);
end
%plot the surface of revolution and the original curve
s = surf(x,y,z);
alpha 0.4
hold on
plot(xx,yy,'k','LineWidth',3)

Matlab IFFT in 3-dimension

I am trying to use Matlab's ifftn in 3-dimensions to get solution in physical space. In particular I am trying to use ifftn on 1/k^2. The analytical solution to that in physical space is 1/(4*pi*r). However I am not recovering that. Please note: $r = sqrt(x^2 + y^2 + z^2)$ and $k = sqrt(kx^2 + ky^2 + kz^2)$.
clc; clear;
n = 128; % no. of points for ifft
L = 2*pi; % size of the periodic domain
x = linspace(-L/2,L/2,n); y = x; z = x; % creating vectors for x-y-z direction
[X,Y,Z] = meshgrid(x,y,z); % creating meshgrid for physical space
R = sqrt(X.^2 + Y.^2 + Z.^2); % use for 1/(4*pi*r)
k = (2*pi/L)*[0:n/2 -n/2+1:-1]; % wave vector;
[k1,k2,k3] = meshgrid(k,k,k);
denom = (k1.^2 + k2.^2 + k3.^2); % This is k^2
F = 1./denom; F(1,1,1) = 0; % The first value is set to zero as it is infinite
phi = 1./(4*pi*R); % physical domain solution
phys = fftshift(ifftn(F)); % Using ifftn
ph_abs = abs(phys);
mid = ph_abs(n/2,:,:); % looking at the midplane of the output
mid = permute(mid,[3 2 1]); % permuting for contourplot.
PHI = phi(n/2,:,:); %looking at the midplane of the physical space.
PHI = permute(PHI,[3 2 1]);
figure(1)
surf(x,z,log(mid))
shading flat
colorbar();
figure(2)
surf(x,z,log10(abs(PHI)))
shading flat
colorbar();

Create a plane of points from a row of points

I am trying to create a "plane", so to speak, of points in MATLAB from a set of initial points. I have so far been able to create only one row of points, with the algorithm shown below:
% Generate molecular orientation and position
a = 4.309; % lattice constant in angstroms
l = 10; % number of lattices desired
placeHolder = [0 0 0 ; a/2 a/2 0; a/2 0 a/2; 0 a/2 a/2]; % centers of molecules
numMol = 4; %input('how many molecules are in the unit cell? \n # molecules = ');
numAtoms = 2; %input('how many atoms per molecule? \n # atoms per molecule = ');
atomPerUC = numMol*numAtoms; % number of atoms per unit cell
dir = zeros(numMol,3); % array for orientations
atomPosition = zeros(numAtoms*l^3,3,numMol); % array for positions of atoms
theta = zeros(numMol,1); % array for theta values
phi = zeros(numMol,1); % array for phi values
b = 1.54; % bond length in angstroms
for kk = 1:numMol % generate unit cell
% disp(['What is the molecular orientation for molecule ',num2str(kk),' ?']);
% dir(kk,1) = input('u = '); % ask for user input for molecular orientations
% dir(kk,2) = input('v = ');
% dir(kk,3) = input('w = ');
dir = [1,1,1;-1,1,1;-1,-1,1;1,-1,1];
u = dir(kk,1); % set variables for theta, phi computation
v = dir(kk,2);
w = dir(kk,3);
theta(kk) = w/sqrt(u^2+v^2+w^2); % theta value for molecule k
if v<0 % phi value for molecule k
phi(kk) = 2*pi - acos(abs(v)/sqrt(u^2+v^2+w^2));
else if v>0
phi(kk) = acos(u/sqrt(u^2+v^2+w^2));
end
end
theta = theta(kk); phi = phi(kk); % set variables for theta, phi for x,y,z computation
xp = placeHolder(kk,1); % cooridnates of center of molecule k
yp = placeHolder(kk,2);
zp = placeHolder(kk,3);
x1 = (b/2)*sin(theta)*cos(phi) + xp; % cooridnates for atoms in molecule
x2 = -(b/2)*sin(theta)*cos(phi) + xp;
y1 = (b/2)*sin(theta)*sin(phi) + yp;
y2 = -(b/2)*sin(theta)*sin(phi) + yp;
z1 = (b/2)*cos(theta) + zp;
z2 = -(b/2)*cos(theta) + zp;
atomPosition(1,:,kk) = [x1 y1 z1];
atomPosition(2,:,kk) = [x2 y2 z2];
end
for k = 1:numMol
x01 = atomPosition(1,1,k); y01 = atomPosition(1,2,k); z01 = atomPosition(1,3,k);
x02 = atomPosition(2,1,k); y02 = atomPosition(2,2,k); z02 = atomPosition(2,3,k);
for ii = 1:l-1
atomPosition(2*ii+1,:,k) = [(atomPosition(2*ii-1,1,k) + a) y01 z01];
atomPosition(2*ii+2,:,k) = [(atomPosition(2*ii,1,k) + a) y02 z02];
end
end
My problem is that I do not know how to, from here, turn this "row" of points into a "plane" of points. This can be thought of as taking the points that are known on the x-axis, and creating similar rows moving up in the y-direction to create an x-y plane of points.
Any help/suggestions would be appreciated!
Though I do not understand exactly what you are trying to do. In a simple case you can move from a row of points to a plane by adding the extra dimension.
ie.
x=[1,2,3,4,5]
y=x^2
changes to
x=[1,2,3,4,5]
y=[1,2,3,4,5]
[x,y] = meshgrid(x,y)
z=x^2+y^2

In matlab, how to use k means to make 30 clusters with circle around each cluster and mark the center?

In matlab, if I have a code which draws a circle and generates 100 random points inside it. I want to use k means to cluster these 100 points into 30 clusters with a circle around each cluster to differentiate between the clusters and i want to mark the center if each cluster.this is the code of the circle and the 100 random points inside it . Any help please ?
%// Set parameters
R =250; %// radius
C = [0 0]; %// center [x y]
N = 100; %// number of points inside circle
%// generate circle boundary
t = linspace(0, 2*pi,100);
x = R*cos(t) + C(1);
y = R*sin(t) + C(2);
%// generate random points inside it
th = 2*pi*rand(N,1);
r = R*randnlimit(0, 1, 0.5, 1, [N 1]);
xR = r.*cos(th) + C(1);
yR = r.*sin(th) + C(2);
%// Plot everything
figure(1), clf, hold on
% subplot(1,N0,1);
plot(x,y,'b');
hold on
text(0,0,'C')
plot(xR,yR,'p')
axis equal
zR=cell(N,1);
for i=1:N
zR{i,1}= [xR(i) yR(i)];
end
m=cell2mat(zR);
function clusterCircle()
%// Set parameters
R = 250; %// radius
C = [0 0]; %// center [x y]
N = 100; %// number of points inside circle
k = 30; %// number of clusters
%// generate circle boundary
t = linspace(0, 2*pi, 100);
x = R*cos(t) + C(1);
y = R*sin(t) + C(2);
%// generate random points inside it
th = 2*pi*rand(N,1);
r = R*randnlimit(0, 1, 0.5, 1, [N 1]);
xR = r.*cos(th) + C(1);
yR = r.*sin(th) + C(2);
%// some simple k-means:
% initial centroids:
% -> use different method, if k > N
% -> can be done more reasonable (e.g. run k-Means for different
% seeds, select seeds equidistant, etc.)
xC = xR(1:k)';
yC = yR(1:k)';
% run:
clusters = zeros(N,1);
clusters_old = ones(N,1);
while sum((clusters - clusters_old).^2) > 0
clusters_old = clusters;
[~,clusters] = min((bsxfun(#minus,xR,xC)).^2 + ...
(bsxfun(#minus,yR,yC)).^2 , [] , 2);
for kIdx = 1:k
xC(kIdx) = mean(xR(clusters==kIdx));
yC(kIdx) = mean(yR(clusters==kIdx));
end
end
%// Plot everything
figure(1);
clf;
hold on;
% -> plot circle and center
text(C(1),C(2),'C');
plot(x,y,'k');
% -> plot clusters
co = hsv(k);
for kIdx = 1:k
% plot cluster points
plot(xR(clusters==kIdx),yR(clusters==kIdx),'p','Color',co(kIdx,:));
% plot cluster circle
maxR = sqrt(max((xR(clusters==kIdx)-xC(kIdx)).^2 + ...
(yR(clusters==kIdx)-yC(kIdx)).^2));
x = maxR*cos(t) + xC(kIdx);
y = maxR*sin(t) + yC(kIdx);
plot(x,y,'Color',co(kIdx,:));
% plot cluster center
text(xC(kIdx),yC(kIdx),num2str(kIdx));
end
axis equal
end
%// creates random numbers, not optimized!
function rn = randnlimit(a,b,mu,sigma,sz)
rn = zeros(sz);
for idx = 1:prod(sz)
searchOn = true;
while searchOn
rn_loc = randn(1) * sigma + mu;
if rn_loc >= a && rn_loc <= b
searchOn = false;
end
end
rn(idx) = rn_loc;
end
end