m_line boundary for stereographic map - matlab

Using M_map package for Matlab, I have created a stereographic projection map of the Arctic. I went on further to create boundary to specify a certain area. Unfortunately, using m_line gives me a straight line from point to point when it should be a curved line. how do I fix this?
figure()
m_proj('stereographic','lat',90,'rad',22,'lon',0)
m_coast('patch',[.5 .5 .5],'edgecolor',[0 0 0]); hold
m_grid ('box', 'fancy', 'ytick',(70:5:90));
[cs,h]=m_tbase('contour',[-250 -1000 -2000 -3000], 'edgecolor', 'k', 'linewidt', 0.00001);
[LongitudeProj, LatitudeProj]=m_ll2xy(longitude, latitude);
bndry_lon=[0 125 125 0 0 ];
bndry_lat=[86 86 90 90 86 ];
m_line(bndry_lon,bndry_lat,'linewi',2,'color','g');
The green line in the image attached is how the code draws the m_line... but the brown line is how it should be drawn since it is a stereographic projection.

I think the function is just made to find points on a map, and connect them with a straight line (As you currently see happening).
A way to get what you want might be to work around this with a polygon that consists of many points, for example:
bndry_lon=[linspace(0,125,100) 125 0 0 ];
bndry_lat=[linspace(86,86,100) 90 90 86 ];
If you believe the function is capable of producing the curved line that you seek directly, please find/create a simple example so we can pick it up from there.

Related

How can I calculate what the phase shift needs to be so I can get two signals with different frequencies to line up at 90 degs or pi/2?

How can I calculate what the phase shift needs to be so I can get two signals with different frequencies to line up at 90 degs or pi/2?
Example 1:
The blue line is 1hz and you can see where the 90 or pi/2 is located (the green vertical line)
The red line is 1.5hz to get both signals to line up with thier corresponding 90 deg phase shifts it looks like I need to shift the red line 30 degrees.
Example 2:
The blue line is 1hz and you can see where the 90 or pi/2 is located (the green vertical line)
The red line is 2hz to get both signals to line up with thier corresponding 90 deg phase shifts it looks like I need to shift the red line 45 degrees.
How can I mathematically calculate what the phase shift needs to be for the red line to have thier corresponing 90 deg phase shifts line up to the blue line if the red line frequency is 0.845hz, 45.453hz, etc...
Ps: I will be doing this in Octave 4.0 which is similar to Matlab.
It's not a matlab question it's just a mathematical question:
you have:
sin(b1*(x-c1)) = sin(b2*(x-c2))
b1*(x-c1) = b2*(x-c2)
c2 = x-(b1*(x-c1))/b2
for c1 = 0, b1 = 1, b2 = 1.5, x = pi/2
c2 = rad2deg(x-(b1*(x-c1))/b2) = 30°

How to create a quadrant-circle shape-matrix in Matlab?

Currently I have a problem with creating a matrix with a quadrant-shape. The problem is as follow:
I would like to have a 138 x 140 matrix with values of 2 and 3. Inside and on the quadrant circle, the values should be 2. Everything outside the circle, I need to have a value of 3. The radius of the circle is 138 (=R138). Hopefully the image below will support my explanation.
I was thinking of using 'triu' (and then flip) in matlab: then I will not get a circle, but a triangle instead (have not tried it yet). And that is not what I want.
What is the simplest way to create this matrix?
You could try this:
a = ones(138, 140)*3;
[gx, gy] = meshgrid(139:-1:0, 137:-1:0);
a(gx.*gx + gy.*gy <= 138*138) = 2;

Coordinates of figure on array

What is the shortest way to find coordinates of figure left/right/top/bottom edges? 4 coordinates (2 horizontal, 2 vertical lines) are enough.
Tried to flip, transpose, etc. My mind is gonna blow :/.
[EDIT]: Image is binary. Figure is represented by 1's.
You can try to get its bounding box with the regionprops() function.
regionprops(img,'BoundingBox')
The result is (x,y) upper left coordinates x_width, y_width, size of the box.
I get [45.5000000000000 45.5000000000000 174 107] in your image.
The shortest solution i made:
% I - image array
V = sum(I,2);
edge_top = find(V,1,'first');
edge_bot = find(V,1,'last');
H = sum(I,1);
edge_left = find(H,1,'first');
edge_right = find(H,1,'last');

Change Default White for Zero On Contour Plot in MATLAB

How can I change the default white for zero for the output of a contour plot to a different color in MATLAB?
longitude = [80 82 95]
latitude = [30 32 35]
temp = [0 0 0; 0 0 0; 0 0 0]
contourf(longitude,latitude,temp)
Thanks,
Amanda
The key seems to be in the "renderer/rendering" mode from the figure properties. It must be set to either 'painters' or 'zbuffer'. I am not an expert and I didn't understand how to set it by default. I changed it manually through the figure editor (click on the outer figure frame --> More properties --> Renderer) and then I clicked "generate code" from the menu bar to make a function that automatically creates all plots in the same way.

distribute same size circles evenly inside a square using Matlab

I have a figure of size 14 x 14 square drawn inside an axis of 20 x 20, in matlab.
I am trying to draw circles of radius 0.7 inside the square and need to arrange them evenly. I need to draw 233 circles. Please let me know how can I do it?
Currently I can draw them randomly but couldn't get 233 circle. Please see my below code.
Your reply is appreciated.
% Urban, sub urban, Rural areas
x_area =[3, 12, 6];
y_area = [6, 8, 16];
r_area = [1, 7, 2];
f = figure;
hAxs = axes('Parent',f);
hold on, box on, axis equal
xlabel('x')
ylabel('y','Rotation',0)
title('Compute the area of circles a vectorized way for several cicles')
axis([0 20 0 20])
rectangle('Position',[5,1,14,14])
rectangle('Position',[3,1,2,2])
rectangle('Position',[1,3,4,4])
hold on, box on, axis equal
a = 233;
x_base_urban = randi([6 18], 1, a);
b = rand([10 8], 1);
y_base_urban = randi([2 14],1, a);
r_base_urban = 0.9;
size_x = size(x_base_urban);
size_x = size_x(2);
size_y = size(y_base_urban);
size_y = size_y(2);
colour = rand(size_x,3);
for t = 1: size_x
plot(x_base_urban(t)+ r_base_urban.*cos(0:2*pi/100:2*pi),...
y_base_urban(t)+ r_base_urban.*sin(0:2*pi/100:2*pi),'parent',hAxs)
plot(x_base_urban(t),y_base_urban(t),'+','parent',hAxs)
end
Thanks
Randomly plotting everything won't work. Actually, if your circles can't overlap, nothing will work. To show that, just compare the results of following calculations:
lSquare = 14;
rCircle = 0.7;
nCircles = 233;
areaCircles = nCircles * pi * rCircle^2
areaSquare = lSquare^2
You will see that areaCircles > areaSquare, so it is impossible to fit them all in. On the other hand, if areaSquare >= areaCircles does not guarantee you that a solution exists!
Try your setup with a smaller example to come up with a solution. E.g. take a square box and a bunch of spherical objects (balls, marbles, oranges, apples, ... if need be) and try to fit as much of those in your box. If that works, you might even want to draw their positions on a sheet of paper before trying to implement it.
If you do this correctly, you will get an idea of how to stack round objects in a square container. That is also exactly what you need to do in your exercise. Then try to make a model/algorithm of what you did manually and implement that in MATLAB. It won't be hard, but you will need some small calculations: Pythagoras and intersection of circles.
I also suggest you use a function to draw a circle as #Andrey shows, so something of the form function drawCircle(center, radius). That allows you to keep complexity down.
If your circles can overlap, then the solution is quite easy: look at a circle as an object with a center point and distribute these center points evenly over the square. Don't use rand to do this, but calculate their positions yourself.
If you can't find a solution, I might expand my answer in a few days.
Without diving too deep into your code, I think that you need to add a hold function, after the first plot
for t = 1: size_x
plot(x_base_urban(t)+ r_base_urban.*cos(0:2*pi/100:2*pi),...
y_base_urban(t)+ r_base_urban.*sin(0:2*pi/100:2*pi),'parent',hAxs)
plot(x_base_urban(t),y_base_urban(t),'+','parent',hAxs)
hold(hAxs,'on');
end
By the way, the best way to draw circle is by using rectangle command.
rectangle('Curvature',[1 1],'Position',[1 3 4 5])
So you can create a PlotCircle function (like #EgonGeerardyn suggests) like this:
function plotCircle(x,y,r)
rectangle('Position',[x-r y-r 2*r 2*r],'Curvature',[1 1]);
end