Matlab Midpoint in 2D domain - matlab

Given a domain (2D grid) and two lines:
D = [-0.99 0.99;-0.99 0.99]; %domain
x = 0.0676 ;
y = 0.7630];
with the lines given as a vector:
pt = [0.0676 0.7630]
what is the most efficient way for me to find the four midpoints in Matlab?
Thank You

If I understand the question right, you're trying to achieve this.
This code does it, you can certainly improve the performance by making better use of vector and matrix operations, but if it's only for a problem this simple the optimization isn't worth it.
dlim_x = [-0.99 0.99];
dlim_y = [-0.99 0.99];
x = 0.0676;
y = 0.7630;
domain_vec_x = [dlim_x(1) dlim_x(1) dlim_x(2) dlim_x(2) dlim_x(1)];
domain_vec_y = [dlim_y(1) dlim_y(2) dlim_y(2) dlim_y(1) dlim_y(1)];
line(domain_vec_x, domain_vec_y);
line([x x], dlim_y, 'linestyle', ':');
line(dlim_x, [y y], 'linestyle', ':');
xlim([1.1*dlim_x]);
ylim([1.1*dlim_y]);
hold on;
x1 = mean([dlim_x(1), x]);
x2 = mean([x, dlim_x(2)]);
y1 = mean([dlim_y(1), y]);
y2 = mean([y, dlim_y(2)]);
plot(x1,y1,'^')
plot(x2,y1,'^')
plot(x1,y2,'^')
plot(x2,y2,'^')

Related

How can I calculate the median in a gamma distribution?

I have the below distribution, which I want to calculate the median of:
x=0:0.01:10;
x=[x' x' x' x' x'];
a=ones(1,1001)';
a=[a*2 a*4 a*6 a*8 a*10];
b=2;
f = gampdf(x,a,b);
plot(x,f)
grid on
User TwistedSim answered my question.
You need to find the value m for which the integral from 0 to m give
you 0.5. You can do that with c = cumsum(f)*dx where dx = 0.01 in your
case. After it's just a matter of using find(c>0.5, 1, 'first').
dx = 0.04;
b=2;
x=kron([0:dx:10]',ones(1,5));
a=kron(ones(size(x,1),1),[2:2:10]);
f = gampdf(x,a,b);
cf = cumsum(f)*dx;
[i,j] = find(cf(1:end-1,:)<0.5 & cf(2:end,:)>=0.5);
cflow = cf(sub2ind(size(x),i ,j));
cfhigh = cf(sub2ind(size(x),i+1,j));
xm = x(i)+dx/2 + (0.5-cflow)./(cfhigh-cflow) * dx
fm = gampdf(xm',a(1,:),b)';
plot(x,f)
hold on; plot([xm xm]',[fm fm]','*'); hold off
grid on

Change the grid points of parametric splines in Matlab

My Code right now
% Create some example points x and y
t = pi*[0:.05:1,1.1,1.2:.02:2]; a = 3/2*sqrt(2);
for i=1:size(t,2)
x(i) = a*sqrt(2)*cos(t(i))/(sin(t(i)).^2+1);
y(i) = a*sqrt(2)*cos(t(i))*sin(t(i))/(sin(t(i))^2+1);
end
Please note: The points (x_i|y_i) are not necessarily equidistant, that's why t is created like this. Also t should not be used in further code as for my real problems it is not known, I just get a bunch of x, y and z values in the end. For this example I reduced it to 2D.
Now I'm creating ParametricSplines for the x and y values
% Spline
n=100; [x_t, y_t, tt] = ParametricSpline(x, y, n);
xref = ppval(x_t, tt); yref = ppval(y_t, tt);
with the function
function [ x_t, y_t, t_t ] = ParametricSpline(x,y,n)
m = length(x);
t = zeros(m, 1);
for i=2:m
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
end
t=t./t(length(t));
x_t = spline(t, x);
y_t = spline(t, y);
t_t = linspace(0,1,n);
end
The plot generated by
plot(x,y,'ob',...
xref,yref,'xk',...
xref,yref,'-r'),...
axis equal;
looks like the follows: Plot Spline
The Question:
How do I change the code so I always have one of the resulting points (xref_i|yref_i) (shown as Black X in the plot) directly on the originally given points (x_j|y_j) (shown as Blue O) with additionally n points between (x_j|y_j) and (x_j+1|y_j+1)?
E.g. with n=2 I would like to get the following:
(xref_1|yref_1) = (x_1|y_1)
(xref_2|yref_2)
(xref_3|yref_3)
(xref_4|yref_4) = (x_2|y_2)
(xref_5|yref_5)
[...]
I guess the only thing I need is to change the definition of tt but I just can't figure out how... Thanks for your help!
Use this as your function:
function [ x_t, y_t, tt ] = ParametricSpline(x,y,nt)
arc_length = 0;
n = length(x);
t = zeros(n, 1);
mul_p = linspace(0,1,nt+2)';
mul_p = mul_p(2:end);
tt = t(1);
for i=2:n
arc_length = sqrt((x(i)-x(i-1))^2 + (y(i)-y(i-1))^2);
t(i) = t(i-1) + arc_length;
add_points = mul_p * arc_length + t(i-1);
tt = [tt ; add_points];
end
t=t./t(end);
tt = tt./tt(end);
x_t = spline(t, x);
y_t = spline(t, y);
end
The essence:
You have to construct tt in the same way as your distance vector t plus add additional nt points in between.

find nearest point to a fitting line and the neighborhood of it in Matlab

Supposed I have two random double array, which means that one x coordinate might have multiple y value.
X = randi([-10 10],1,1000);
Y = randi([-10 10],1,1000);
Then I give a line equation: y=ax+b.
I want to find the nearest point to the nearest point to the line based on every x point. And when I find this point, I will find it's neighborhood points within specific range. Please forgive my poor English, maybe following picture can help more.
Because I have a lot of data points, I hope there is an efficient way to deal with this problem.
if your X's are discrete you can try something like:
xrng = [-10 10];
yrng = [-10 10];
a = rand;
b = rand;
f = #(x) a*x + b;
X = randi(xrng,1,1000);
Y = randi(yrng,1,1000);
ezplot(f,xrng);
hold on;
plot(X,Y,'.');
xx = xrng(1):xrng(2);
nbrSz = 2;
nx = diff(xrng);
nearestIdx = zeros(nx,1);
nbrIdxs = cell(nx,1);
for ii = 1:nx
x = xx(ii);
y = f(x);
idx = find(X == x);
[~,idxidx] = min(abs(y - Y(idx)));
nearestIdx(ii) = idx(idxidx);
nbrIdxIdxs = abs(Y(nearestIdx(ii)) - Y(idx)) <= nbrSz;
nbrIdxs{ii} = idx(nbrIdxIdxs);
plot(X(nearestIdx(ii)),Y(nearestIdx(ii)),'og');
plot(X(nearestIdx(ii)) + [0 0],Y(nearestIdx(ii)) + [-nbrSz nbrSz],'g')
plot(X(nbrIdxs{ii}),Y(nbrIdxs{ii}),'sy')
end

Plotting a 2d vector on 3d axes in matlab

I have three vectors x,y,t. For each combination x,y,t there is a (u,v) value associated with it. How to plot this in matlab? Actually I'm trying to plot the solution of 2d hyperbolic equation
vt = A1vx + A2vy where A1 and A2 are 2*2 matrices and v is a 2*1 vector. I was trying scatter3 and quiver3 but being new to matlab I'm not able to represent the solution correctly.
In the below code I have plot at only a particular time-level. How to show the complete solution in just one plot? Any help?
A1 = [5/3 2/3; 1/3 4/3];
A2 = [-1 -2; -1 0];
M = 10;
N = 40;
delta_x = 1/M;
delta_y = delta_x;
delta_t = 1/N;
x_points = 0:delta_x:1;
y_points = 0:delta_y:1;
t_points = 0:delta_t:1;
u = zeros(M+1,M+1,N+1,2);
for i=1:M+1,
for j=1:M+1,
u(i,j,1,1) = (sin(pi*x_points(i)))*sin(2*pi*y_points(j)) ;
u(i,j,1,2) = cos(2*pi*x_points(i));
end
end
for j=1:M+1,
for t=1:N+1,
u(M+1,j,t,1) = sin(2*t);
u(M+1,j,t,2) = cos(2*t);
end
end
for i=1:M+1
for t=1:N+1
u(i,1,t,1) = sin(2*t);
u(i,M+1,t,2) = sin(5*t) ;
end
end
Rx = delta_t/delta_x;
Ry = delta_t/delta_y;
for t=2:N+1
v = zeros(M+1,M+1,2);
for i=2:M,
for j=2:M,
A = [(u(i+1,j,t-1,1) - u(i-1,j,t-1,1)) ; (u(i+1,j,t-1,2) - u(i-1,j,t-1,2))];
B = [(u(i+1,j,t-1,1) -2*u(i,j,t-1,1) +u(i-1,j,t-1,1)) ; (u(i+1,j,t-1,2) -2*u(i,j,t-1,2) +u(i-1,j,t-1,2))];
C = [u(i,j,t-1,1) ; u(i,j,t-1,2)];
v(i,j,:) = C + Rx*A1*A/2 + Rx*Rx*A1*A1*B/2;
end
end
for i=2:M,
for j=2:M,
A = [(v(i,j+1,1) - v(i,j-1,1)) ; (v(i,j+1,2) - v(i,j-1,2)) ];
B = [(v(i,j+1,1) - 2*v(i,j,1) +v(i,j-1,1)) ; (v(i,j+1,2) - 2*v(i,j,2) +v(i,j-1,2))];
C = [v(i,j,1) ; v(i,j,2)];
u(i,j,t,:) = C + Ry*A2*A/2 + Ry*Ry*A2*A2*B/2;
end
end
if j==2
u(i,1,t,2) = u(i,2,t,2);
end
if j==M
u(i,M+1,t,1) = u(i,M,t,1);
end
if i==2
u(1,j,t,:) = u(2,j,t,:) ;
end
end
time_level = 2;
quiver(x_points, y_points, u(:,:,time_level,1), u(:,:,time_level,2))
You can plot it in 3D, but personally I think it would be hard to make sense of.
There's a quiver3 equivalent for your plotting function. z-axis in this case would be time (say, equally spaced), and z components of the vectors would be zero. Unlike 2D version of this function, it does not support passing in coordinate vectors, so you need to create the grid explicitly using meshgrid:
sz = size(u);
[X, Y, Z] = meshgrid(x_points, y_points, 1:sz(3));
quiver3(X, Y, Z, u(:,:,:,1), u(:,:,:,2), zeros(sz(1:3)));
You may also color each timescale differently by plotting them one at a time, but it's still hard to make sense of the results:
figure(); hold('all');
for z = 1:sz(3)
[X, Y, Z] = meshgrid(x_points, y_points, z);
quiver3(X, Y, Z, u(:,:,z,1), u(:,:,z,2), zeros([sz(1:2),1]));
end

MATLAB style griddedInterpolant usage from Interpolations.jl

I'm trying to convert a MATLAB program of mine to Julia. A key feature of this program uses the griddedInterpolant function in MATLAB. I have found the Julia replacement (Interpolations.jl) and I have done a simple test in the 2-dimensional case to be sure that I understand how it works. This particular program uses 4-D arrays though, and I can't seem to figure out how to work the Interpolations.jl method beyond 2 dimensions.
A very simplified example of the MATLAB behavior. Note that this is a trivial example because V is flat, but you get the point. What my program would do is be changing the values of V, kp1gv, and kp2gv within a loop.
nK_1 = 50;
nK_2 = 50;
nKP_1 = 10;
nKP_2 = 10;
K1 = linspace(0,100,nK_1);
K2 = linspace(0,100,nK_2);
KP1 = linspace(0,100,nKP_1);
KP2 = linspace(0,100,nKP_1);
nX = 3;
nY = 3;
X = [0.9,1,1.1];
Y = [0.95,1,1.05];
[k1gv,k2gv,xgv,ygv] = ndgrid(K1,K2,X,Y); % creates grid vectors
V = ones(nK_1,nK_2,nX,nY); % value func to be interpolated
Fit = griddedInterpolant(k1gv,k2gv,xgv,ygv,V,'linear'); % fitted val fun
[kp1gv,kp2gv,xpgv,ypgv,kk1,kk2] = ndgrid(KP1,KP2,X,Y,K1,K2);
Fitted = Fit(kp1gv,kp2gv,xpgv,ypgv);
What I am looking for is to duplicate this in Julia, either exactly as it is in MATLAB or by rewriting it to be faster. Here is my attempt now:
nK_1 = 50
nK_2 = 50
nKP_1 = 10
nKP_2 = 10
K1 = linspace(0,100,nK_1)
K2 = linspace(0,100,nK_2)
KP1 = linspace(0,100,nKP_1)
KP2 = linspace(0,100,nKP_1)
nX = 3
nY = 3
X = [0.9,1,1.1]
Y = [0.95,1,1.05]
V = ones(nK_1,nK_2,nX,nY)
# using the more compact notation here, would be similar to
# griddedInterpolant({K1,K2,X,Y},V,'linear') -- no issues with this fit
Fit = interpolate((K1,K2,X,Y),V,Gridded(Linear()))
# this is how I'm getting ndgrid functionality
KP1gv = Float64[i for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
KP2gv = Float64[j for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
Xgv = Float64[x for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
Ygv = Float64[y for i in KP1, j in KP2, x in X, y in Y, a in K1, b in K2]
# what I want to work
Fitted = Fit[KP1gv,KP2gv,Xgv,Ygv]
I've read the documentation on Interpolations.jl, and I know this should be doable, I just can't seem to get it to work.
Rather than construct all those ndgrid coordinate arrays, just do this:
fitted = [Fit[i,j,k,l] for i in KP1, j in KP2, k in X, l in Y]
But a couple of tips:
Anything performance-critical (like the line above) should be placed in a function. This is absolutely crucial. See the performance tips page of the manual.
This seems not to be well-documented, but Gridded is designed for cases where you have a rectangular grid, but where the spacing may not be regular. In cases of regular spacing, you can do better with
itp = interpolate(V, Linear(), OnGrid())
sitp = scale(itp, K1, K2, x, y)
where x and y are also linspace objects.