How can I figure out and mark the highest value in my diagram? - matlab

Update my modified code -> the wrong point is marked! see the colorbar! :)
Input_Matrix = textread('Rainflow_Input1.txt')
[zeilen,spalten]=size(Input_Matrix)
x = Input_Matrix(:,1)
y = Input_Matrix(:,2)
z = Input_Matrix(:,3)
colorbar('location','Manual', 'position', [0.93 0.1 0.02 0.81]);
az = 0;
el = 90;
view(az, el);
%scatter3(x,y,z,'filled')%Problem i dont know to make it filled
view(0,90)% view from above !!!http://de.mathworks.com/help/matlab/ref/view.html
a = 30;%markersize
scatter3(x, y, z, a, z, 'filled');
view(0, 90)
idx = find(max(z)) ;
hold on
plot3(x(idx),y(idx),z(idx),'*r')
colorbar
datacursormode on
How can I figure out the highest value in my diagram? I want to mark it in my diagram. So the x y and z value should be displayed from the highest value -> z!. Thank you for your help guys. I really appreciate it.
Input_Matrix = textread('Rainflow_Input1.txt')
[zeilen,spalten]=size(Input_Matrix)
x = Input_Matrix(:,1)
y = Input_Matrix(:,2)
z = Input_Matrix(:,3)
colorbar('location','Manual', 'position', [0.93 0.1 0.02 0.81]);
az = 0;
el = 90;
view(az, el);
%scatter3(x,y,z,'filled')%Problem i dont know to make it filled
view(0,90)% view from above !!!http://de.mathworks.com/help/matlab/ref/view.html
a = 30;%markersize
scatter3(x, y, z, a, z, 'filled');
view(0, 90)
colorbar;
datacursormode on
;
My input:
-220.8 228 50045
-222 201.6 50045
-220.2 198 200176
-224.4 196.8 200176
-220.8 192 200176
-221.4 190.8 50044
-226.2 176.4 200176
-199.2 156 50044
-201.6 153.6 50045
-219 147.6 50044
-252.6 133.2 50044
-210 129.6 200176
-250.8 127.2 50044
-201 126 50044
-229.2 124.8 50044
-183 123.6 200176
-168 122.4 200176
-275.4 118.8 200176
-261 13.2 400352
-259.8 13.2 200176
-258.6 13.2 200176
-257.4 13.2 290176

The maximum value (of z I assume) of your data will be m in index ind
[m,ind]=max(z);
If you are worried that there are more than one, you can then always do
indexes=find(z==m);
To mark them, after the call to scatter3 do a hold on and call scatter3 just with the maximum valued data, and another format e.g. '*' and maybe some other color

Related

How do I create an animation for a rocket using the Aerospace Toolbox

How do I create an animation for a rocket using xyz, roll, pitch, yaw and time in MATLAB Aerospace Toolbox?
Here is some sample data:
x = 1.0e+06 .*[3.0138, 3.1345, 3.7675, 4.7347,6.1352];
y = 1.0e+07 .*[-1.8160,-1.8244,-1.8326,-1.8232,-1.7877];
z = 1.0e+07 .*[0.9917,0.9980,1.0119,1.0218,1.0261];
r =[ 0,0,0.0046,0.0046,0.0046];
p =[ 89.9900,26.6402,22.4665,16.0608,3.6879];
y =[86.7370,86.7370,86.7810,86.7810,86.7810];
t =[0,95,186,282,380];
Here is what I have tried so far:
data = [x',y',z',r',p',y',t'];
h=Aero.Animation;
f=figure;
h.Figure=f;
h.initialize();
h.FramesPerSecond=10
h.TimeScaling = 5;
idx1=h.createBody('delta2.ac','ac');
h.show()
h.bodies{1}.TimeseriesSourceType='Array6DoF';
h.bodies{1}.timeseriesSource=data;
h.Camera.offset=[-150 -150 0];
h.show()
h.VideoRecord = 'on';
h.VideoQuality = 50;
h.VideoCompression = 'Motion JPEG AVI'
h.VideoFilename = 'rocket trajectory';
h.play()
h.VideoRecord='off';
However when I try to run the data, the rocket doesnt rotate or translate.
Created a working animation using the code below:
6dof data should be in this format as an array: [ time, lat, lon, alt, phi , theta, psi]
Make sure that phi, theta, psi are in radians not degrees.
%sixdof_data = [time,lat,long,alt,phi,theta,psi]; % This is if you have data saved as variables
h = Aero.Animation;
h.FramesPerSecond = 20;
h.TimeScaling = 10;
h.createBody('delta2.ac');
h.bodies{1}.TimeseriesSourceType='Array6DoF';
h.Bodies{1}.TimeSeriesSource = sixdof_data;
h.Camera.PositionFcn = #staticCameraPosition;
h.Camera.ViewAngle = 6;
h.show();
h.VideoRecord = 'on';
h.VideoQuality = 50;
h.VideoCompression = 'Motion JPEG AVI';
h.VideoFilename = 'rocket trajectory'
h.play();
useful docs:
https://www.mathworks.com/help/aerotbx/ug/playaero.flightgearanimation.html
Here is some sample data that should work:
sixdof_data =[
0 28.534 279.43 2.8871e-09 0 1.5706 1.6397
73 28.529 279.51 23057 0 0.88894 1.6397
148 28.478 280.3 1.6241e+05 0 0.70976 1.6397
220 28.36 281.87 3.8062e+05 8.3012e-05 0.44196 1.6405
294 28.185 283.85 5.206e+05 8.3012e-05 0.38282 1.6405
368 27.936 286.3 5.9145e+05 8.3012e-05 0.32057 1.6405
443 27.575 289.41 6.137e+05 8.3012e-05 0.20756 1.6405
517 27.055 293.39 6.0892e+05 8.3012e-05 0.094718 1.6405]

Can a MATLAB contour plot look like the one from Mathematica?

I am trying to plot a contour plot generated by Mathematica and repeat this in MATLAB.
MATLAB Code:
bx = readmatrix('BX X,Z.xlsx');
bz = readmatrix('BZ X,Z.xlsx');
B=sqrt(bx.^2+bz.^2);
x=-12.4:0.5:12.6;
y=24:-2:0;
[Y,X]=meshgrid(x,y);
%surf(Y,X,transpose(B));
%hold on;
contour(Y,X,transpose(B),'fill','on');
%colormap(parula(18));
xlabel('X-axis');
ylabel('Z-axis');
zlabel('B-value');
title('the value of B data');
colorbar;
This results in the following figure which looks like an inverted rainbow.
The expected result is a plot like this one from Mathematica:
The Mathematica code that generated the desired figure is below.
dataBx = Import["BvsY_1207.xlsx"] [[1]];
dataBz = Import["BvsY_1207.xlsx"] [[2]];
dataBz = dataBz + .06;
ClearAll[Bx, Bz, x, z];
listx = Range[0, 25, 0.5];
listx = listx - 12.4;
x = Transpose[Table[listx, {i, 13}]];
listy = 12 - Range[0, 24, 2];
y = Table[listy, {i, 51}];
dataB = Sqrt[dataBx^2 + dataBz^2];
B = Transpose[{Flatten[y], Flatten[x], Flatten[dataB]}];
ListContourPlot[B, InterpolationOrder -> 4, Contours -> 18,
ContourLines -> False,
BaseStyle -> {FontFamily -> "Times", FontSize -> 24},
FrameLabel -> {"Y/mm", "X/mm", "" , "" }, PlotLegends -> Automatic]
Source data: Excel files for Matlab code
You can define your own color map and the number of different colors for each area of the contour plot. I just took 6 colors from the Mathematica plot and extrapolated them into 20 points.
% custom color map
cm = [41 85 141;
100 112 149;
168 135 109;
231 163 82;
245 197 122;
255 236 181]/255;
k = 6; %number of custommap points
n = 20; %number of filled areas on the plot
ls = linspace(1, k, n);
% interpolation of the color map for n elements
cm_int = horzcat(interp1(1:k, cm(:, 1)', ls)', interp1(1:k, cm(:, 2)', ls)', interp1(1:k, cm(:, 3)', ls)');
B=sqrt(bx.^2+bz.^2);
x=-12.4:0.5:12.6;
y = 0:2:24;
[Y,X]=meshgrid(x,y);
[C,h] = contourf(Y,X,transpose(B), n);
set(h,'LineColor','none')
xlabel('X-axis');
ylabel('Z-axis');
zlabel('B-value');
title('the value of B data');
colormap(cm_int);
colorbar;

Connecting a set of points to get a non-self-intersecting non-convex polygon

I have an unordered set of 2D points which represents the corners of a building. I need to connect them to get the outline of the building.
The points were obtained by combining different polygons collected by different individuals. My idea is to use these polygons to get the points in order (e.g. taking the region between the biggest and smallest polygons and connect the points such that it comes in this region).
I tried using the minimum distance criteria and also to connect the points based on angles. But unfortunately it doesn't work. One useful thing which I have is the raw data of many polygons in which the point order is correct. So is there any possibility to compare with those polygons to connect these points? As I mentioned above, my professor gave the idea to take the biggest and the smallest polygons and use the area in between as a buffer. All the points will fall in this buffer area. But I am not sure how to implement this.
X = [364.533 372.267 397.067 408.133 382.471 379.533 329.250 257.200 199.412 195.267 184.385 168.643 157.533 174.500 108.533 99.333 150.733 184.800 138.105 179.474 218.278 232.133 267.714 306.929 312.143 357.733 421.333 431.000 371.867 364.533];
Y = [192.027 233.360 228.627 286.693 314.541 292.960 327.450 340.500 348.671 326.693 269.308 330.857 274.493 226.786 239.200 193.467 182.760 101.893 111.000 80.442 74.356 140.360 64.643 56.857 77.786 69.493 133.293 180.427 142.160 192.027];
The expected result is a closed polygon which represents the plan of the building. I have 15 building samples and the code needs to work for all. Some buildings don't preserve the right angle criteria between the corners. I am attaching the data which i had. The points i have is obtained by integrating the polygons. So is there any way to use this polygons(in which the points are in order)actual data before integration
EDIT
So, I could find a solution using my below-mentioned idea.
Remarks: I added one missing point manually. And, I removed the two tiny corners at the bottom. Either, these must be four corners in total, or they can be treated as no corners at all.
I explicitly state that, because my idea incorporates the assumption, that corners typically have a 90 degree angle.
General approach
Find order of points by below-mentioned method.
For all points, determine potential "neighbours" within a limit with respect to the found order.
For each two neighbours, determine the angle between neighbour #1 - current point - neighbour #2. Ideally, this angle should be 90 degree.
For all candidate combinations, find the one with the minimal total distance, i.e. distance(neighbour #1 - current point) + distance(current point - neighbour #2).
I realized that by using a for loop over all points, resulting in drawing all lines two times. Also, a lot of the calculations might by vectorized and moved from the loop. Optimization wasn't my intention right now. ;-)
% Point data of building corners; modified!
X = [285.400 372.267 397.067 408.133 382.471 379.533 199.412 195.267 184.385 168.643 157.533 174.500 108.533 99.333 150.733 184.800 138.105 179.474 218.278 232.133 267.714 306.929 312.143 357.733 421.333 431.000 371.867 364.533];
Y = [130.150 233.360 228.627 286.693 314.541 292.960 348.671 326.693 269.308 330.857 274.493 226.786 239.200 193.467 182.760 101.893 111.000 80.442 74.356 140.360 64.643 56.857 77.786 69.493 133.293 180.427 142.160 192.027];
% Place approximative center of building at (0, 0)
X = X - mean(X);
Y = Y - mean(Y);
C = [mean(X), mean(Y)];
% Sort points by angle with respect to center
[~, idx] = sort(atan2(X, Y));
% Rearrange points with respect to sorted angles
X = X(idx);
Y = Y(idx);
% Number of data points
n = numel(X);
% Calculate direction vectors for X and Y coordinates
dvX = repmat(X.', 1, n);
dvX = dvX - dvX.';
dvY = repmat(Y.', 1, n);
dvY = dvY - dvY.';
% Calculate distances
dst = sqrt(dvX.^2 + dvY.^2);
% Number of "neighbouring" points to be considered with respect to the order
nn = 8;
figure(1);
hold on;
% Center
plot(C(1), C(2), 'kx', 'MarkerSize', 15);
% Plain points
plot(X, Y, '.', 'MarkerSize', 15);
for k = 1:n
% Index
text(X(k) + 0.05, Y(k) + 0.05, num2str(k), 'FontSize', 12);
% Set up neighbourhood
nbh = mod([k-nn/2:k-1 k+1:k+nn/2], n);
nbh(nbh == 0) = n;
% Calculate angles and total distance arrays
ang = Inf(nn);
len = Inf(nn);
for ii = 1:nn
l = nbh(ii);
d1 = [dvX(k, l) dvY(k, l)];
for jj = ii+1:nn
m = nbh(jj);
d2 = [dvX(k, m) dvY(k, m)];
len(ii, jj) = dst(k, l) + dst(k, m);
ang(ii, jj) = abs(pi/2 - acos(dot(d1, d2) / (norm(d1) * norm(d2))));
end
end
% Find candidates with angle difference < 10 degree
cand = find(ang < pi/18);
% For these candidates, find the one with the shortest total distance
[~, I] = min(len(cand));
% Get corresponding indices
[I, J] = ind2sub([nn nn], cand(I));
cand = nbh([I J]);
% Lines
plot([X(k) X(cand(1))], [Y(k) Y(cand(1))], 'b', 'LineWidth', 1);
plot([X(k) X(cand(2))], [Y(k) Y(cand(2))], 'b', 'LineWidth', 1);
end
hold off;
Output image:
An approximative(!) solution is to determine the center of the contour described by the found points, and use atan2 with respect to the center to order the points by angle. See the following code snippet for visualization:
% Points
X = 2 * rand(1, 15) - 1;
Y = 2 * rand(1, 15) - 1;
% Center
C = [0, 0];
% Determine indices
[~, idx] = sort(atan2(X, Y));
figure(1);
hold on;
% Center
plot(C(1), C(2), 'kx', 'MarkerSize', 15);
% Plain points
plot(X, Y, '.', 'MarkerSize', 15);
% Indices and lines
for k = 1:numel(X)
text(X(idx(k)) + 0.05, Y(idx(k)) + 0.05, num2str(k), 'FontSize', 12);
if (k == numel(X))
plot([X(idx(k)) X(idx(1))], [Y(idx(k)) Y(idx(1))], 'b');
else
plot([X(idx(k)) X(idx(k+1))], [Y(idx(k)) Y(idx(k+1))], 'b');
end
end
hold off;
Gives the following output:
Although I'm sure, that a certain amount of the concavities will be correctly handled, I'm afraid, that it'll fail for the given example (especially the upper part). This is, because the image is not a perfect top view, thus angles are kinda "distorted".
Nevertheless, maybe the ordering can boost your minimum distance approach.
Here's a solution which that good for shapes that have outlines made from perpendicular* lines (as the one in your example). The idea is as follows:
We rotate the points to align* them to the XY grid.
We group points into families that have either the same* X or Y coordinates.
For each point we compute two points: the closest horizontally, and the closest vertically, from within the allowed families.
Build a connectivity matrix and transform back.
Just like in HansHirse's answer, I must change the dataset: add a missing corner (pt. 30), remove two non-corners (pts. 7-8), remove the duplicate last point.
* - approximately.
function A = q55511236
%% Initialization:
% Define points:
X = [364.533 372.267 397.067 408.133 382.471 379.533 329.250 257.200 199.412 195.267 184.385 ...
168.643 157.533 174.500 108.533 99.333 150.733 184.800 138.105 179.474 218.278 232.133 ...
267.714 306.929 312.143 357.733 421.333 431.000 371.867];
Y = [192.027 233.360 228.627 286.693 314.541 292.960 327.450 340.500 348.671 326.693 269.308 ...
330.857 274.493 226.786 239.200 193.467 182.760 101.893 111.000 80.442 74.356 140.360 ...
64.643 56.857 77.786 69.493 133.293 180.427 142.160];
%% Preprocessing:
% Centering:
XY = [X;Y] - [mean(X); mean(Y)];
% Rotation:
[U,~,~] = svd(XY,'econ');
rXY = (U.' * XY).';
% Fixing problems w/ some points:
rXY = vertcat(rXY, [-21.8, 66]); % add missing point
rXY(7:8, :) = NaN; % remove non-corners
% figure(); scatter(rXY(:,1),rXY(:,2));
%% Processing:
% Group points according to same-X and same-Y
CLOSE_ENOUGH_DISTANCE = 10; % found using trial and error
[~,~,sameXpts] = uniquetol(rXY(:,1), CLOSE_ENOUGH_DISTANCE, 'DataScale', 1);
[~,~,sameYpts] = uniquetol(rXY(:,2), CLOSE_ENOUGH_DISTANCE, 'DataScale', 1);
% Create masks for distance evaluations:
nP = size(rXY,1);
[maskX,maskY] = deal(zeros(nP));
maskX(sameXpts == sameXpts.') = Inf;
maskY(sameYpts == sameYpts.') = Inf;
% Compute X and Y distances separately (we can do this in the rotated space)
dX = abs(rXY(:,1) - rXY(:,1).') + maskX + 1./maskY;
dY = abs(rXY(:,2) - rXY(:,2).') + maskY + 1./maskX;
[~,nX] = min(dX);
[~,nY] = min(dY);
% Construct connectivity matrix:
A = false(nP);
idxTrue = sub2ind(size(A), repmat(1:nP, [1,2]), [nX(:).', nY(:).']);
A(idxTrue) = true;
%% Plot result:
% Rotated coordinates:
figure(); gplot(A, rXY, '-o'); text(rXY(:,1), rXY(:,2), string(1:nP));
uXY = (U*rXY.').';
% Original coordinates:
figure(); gplot(A, uXY, '-o'); text(uXY(:,1), uXY(:,2), string(1:nP)); axis ij;
Resulting in:
The concept used for the answer is the 'Travelling salesman problem'. A buffer is created around the points and this buffer is included as an extra criterion.
a=[141 188 178 217 229 282 267 307 313 357 372 422 434 365 372 398 411 382 382 233 229 191 185 166 156 183 173 114 97 149 139 139];
b=[109 103 79 76 140 132 64 56 78 72 141 133 180 192 234 228 287 293 315 348 343 348 329 332 270 268 225 240 194 184 108 108];
X=[364.5333 232.1333 397.0667 157.5333 431 421.3333 306.9286 184.3846 357.7333 199.4118 168.6429 179.4737 408.1333 382.4706 150.7333 372.2667 184.8 138.1053 312.1429 108.5333 174.5 195.2667 257.2 99.33333 379.5333 371.8667 329.25 280.7059 267.7143 218.2778];
Y=[192.0267 140.36 228.6267 274.4933 180.4267 133.2933 56.85714 269.3077 69.49333 348.6706 330.8571 80.44211 286.6933 314.5412 182.76 233.36 101.8933 111 77.78571 239.2 226.7857 326.6933 340.5 193.4667 292.96 142.16 327.45 130.5529 64.64286 74.35556];
R = [a' b'];
d = 12;
polyout = polybuffer(R,'lines',d)
figure
%imshow(I2);
hold on
%plot(R(:,1),R(:,2),'r.','MarkerSize',10)
plot(X,Y,'r.', 'MarkerSize', 15)
plot(polyout)
axis equal
hold off
[s,t] = boundary(polyout); %%this is the boundary polygon of the buffer
numPoints = length(clustersCentroids);
x = X; %these are the points to be connected
y = Y;
x([1 2],:)=x([2 1],:);
y([1 2],:)=y([2 1],:);
figure
plot(x, y, 'bo', 'LineWidth', 2, 'MarkerSize', 17);
grid on;
imshow(I2);
xlabel('X', 'FontSize', 10);
ylabel('Y', 'FontSize', 10);
% Make a list of which points have been visited
beenVisited = false(1, numPoints);
% Make an array to store the order in which we visit the points.
visitationOrder = ones(1, numPoints);
% Define a filasafe
maxIterations = numPoints + 1;
iterationCount = 1;
% Define a current index. currentIndex will be 1 to start and then will vary.
currentIndex = 1;
while sum(beenVisited) < numPoints
visitationOrder(iterationCount) = currentIndex;
beenVisited(currentIndex) = true;
% Get the x and y of the current point.
thisX = x(currentIndex);
thisY = y(currentIndex);
%text(thisX + 0.01, thisY, num2str(currentIndex), 'FontSize', 35, 'Color', 'r');
% Compute distances to all other points
distances = sqrt((thisX - x) .^ 2 + (thisY - y) .^ 2);
distances(beenVisited)=inf;
distances(currentIndex) = inf;
% Don't consider visited points by setting their distance to infinity.
[out,idx] = sort(distances);
xx=[x(currentIndex) x(idx(1))]
yy=[y(currentIndex) y(idx(1))]
if isempty(polyxpoly(xx,yy,s,t))
iterationCount = iterationCount + 1;
currentIndex =idx(1);
else
xx=[x(currentIndex) x(idx(2))]
yy=[y(currentIndex) y(idx(2))]
if isempty(polyxpoly(xx,yy,s,t))
iterationCount = iterationCount + 1;
currentIndex =idx(2);
else
xx=[x(currentIndex) x(idx(3))]
yy=[y(currentIndex) y(idx(3))]
if isempty(polyxpoly(xx,yy,s,t))
iterationCount = iterationCount + 1;
currentIndex =idx(3);
else
xx=[x(currentIndex) x(idx(4))]
yy=[y(currentIndex) y(idx(4))]
if isempty(polyxpoly(xx,yy,s,t))
iterationCount = iterationCount + 1;
currentIndex =idx(4);
end
end
end
end
end
% Plot lines in that order.
hold on;
orderedX = [x(visitationOrder); x(1)];
orderedY = [y(visitationOrder) ;y(1)];
plot(orderedX,orderedY, 'm-', 'LineWidth', 2);
title('Result', 'FontSize', 10);

Matlab Dissecting Extracting Camera Projection Matrix to position and rotation

I have 6 points in space with known coordinates in mm and corresponding 2D pixel coordinates in the image (image size is 640x320 pixels and points coordinates have been measured from upper left of the image. also I have the focal length of the camera as 43.456mm. trying to find the camera position and orientation.
My matlab code here will give me the camera location as -572.8052
-676.7060 548.7718 and seems correct but I am having a hard time finding the orientation values (yaw pitch roll of the camera in degrees)
I know that the rotation values should be 60.3,5.6,-45.1
the last 4 lines in my code needs to be updated to output the orientation of the camera.
I would really really appreciate your help on this.
Thanks.
Here is my matlab code:
Points_2D= [135 183 ; 188 129 ; 298 256 ; 301 43 ; 497 245; 464 110];
Points_3D= [-22.987 417.601 -126.543 ; -132.474 37.67 140.702 ; ...
388.445 518.635 -574.784 ; 250.015 259.803 67.137 ; ...
405.915 -25.566 -311.834 ; 568.859 164.809 -162.604 ];
M = [0;0;0;0;0;0;0;0;0;0;0];
A = [];
for i = 1:size(Points_2D,1)
u_i = Points_2D(i,1);
v_i = Points_2D(i,2);
x_i = Points_3D(i,1);
y_i = Points_3D(i,2);
z_i = Points_3D(i,3);
A_vec_1 = [x_i y_i z_i 1 0 0 0 0 -u_i*x_i -u_i*y_i -u_i*z_i -u_i]; %
A_vec_2 = [ 0 0 0 0 x_i y_i z_i 1 -v_i*x_i -v_i*y_i -v_i*z_i -v_i]; %
A(end+1,:) = A_vec_1;
A(end+1,:) = A_vec_2;
end
[U,S,V] = svd(A);
M = V(:,end);
M = transpose(reshape(M,[],3));
Q = M(:,1:3);
m_4 = M(:,4);
Center = (-Q^-1)*m_4
k=[43.456/640 0 320 ;0 43.456/320 160;0 0 1 ];
Rotation= (Q^-1)*k;
CC=Rotation'
eul=rotm2eul(CC)
First thing first: 6 points are enough but it is likely that you have some error. To get a better performance, it is recommended to have more than 6 points, like 10-15 preferably.
Your code seems correct until:
Q = M(:,1:3);
m_4 = M(:,4);
So you are looking for extrinsic and intrinsic parameters of the camera, i.e. rotation, translation, alpha(skew in x direction), ro, beta(skew in x direction), u0, v0 (translation of camera center). So a total of 5 intrinsics, 6 extrinsic parameters.
Here is a link which explains the details how to calculate those parameters. I had a code which I didn't test thoroughly, may be some errors but it was working in my case.
Continuing from M which is the 3x4 matrix you found:
a1 = M(1, 1:3);
a2 = M(2, 1:3);
a3 = M(3, 1:3);
b = M(1:3,4);
% Decomposition of the parameters
eps = 1; %this can be -1 or +1, based on the value you choose, you will have two different results.
ro = eps/sqrt(sumsqr(a3));
r3 = ro*a3;
u0 = ro.^2*(dot(a1,a3))
v0 = ro.^2*(dot(a2,a3))
cos_theta = -dot(cross(a1,a3),cross(a2,a3))/ ...
dist_cross(a1,a3)/(dist_cross(a2,a3));
theta = acos(cos_theta);
alpha = ro^2*dist_cross(a1,a3)*sin(theta)
beta = ro^2*dist_cross(a2,a3)*sin(theta)
theta_deg = theta*180/pi
r1 = 1/dist_cross(a2,a3)*cross(a2,a3);
r2 = cross(r3,r1);
R = [r1;r2;r3] % Rotation matrix 3x3
% ro*A.inv(R) = K
K = [alpha -alpha*cot(theta) u0;
0 beta/sin(theta) v0;
0 0 1 ];
T = ro*(inv(K)*b) % Translation matrix, 1x3
where
function [dis] = dist_cross(mi,mj)
dis = sqrt(sumsqr(cross(mi,mj)));
end
I don't guarantee it is totally correct but it should help.

Interpolating the end points in Matlab's polarPlot

I managed connect the end points in the normal polarPlot like
data([1:end 1],1)
but doing interpolation does not interpolate the extended path
data = load('rem_angle_2.dat');
n = 30; phi = interp(data([1:end 1],1)*pi/180, n); H = interp(data([1:end 1], 3), n);
mu = 4 * 3.14e-7; ms = 1.2e6; K = 4.5e4; h = mu .* ms .* H / (2 .* K);
cosphi = h .* abs(cos( phi )) + (cos( phi ) ) .^2;
polar(phi, cosphi, 'r-x');
Example output in the red circle
Data
0 0.0314410000000000 0.940571096308908
15 0.0349230000000000 0.969954146597296
30 0.0313780000000000 0.839337718198396
45 0.0248700000000000 0.745214472624085
60 0.0231580000000000 0.478142525177060
75 0.0199550000000000 0.296548109978132
90 0.0270400000000000 0.155780680534267
105 0.0203080000000000 0.344801658689296
120 0.0254600000000000 0.592786274973634
135 0.0290010000000000 0.754378087574740
150 0.0238800000000000 0.834979038161321
165 0.0208110000000000 1.07503919352428
180 0.0312170000000000 0.950446840529786
210 0.0270380000000000 0.825443882649447
240 0.0321900000000000 0.588919403368673
270 0.0312300000000000 0.0490005355090298
300 0.0243250000000000 0.486928993377883
330 0.0257870000000000 0.846981230530059
How can you interpolate the end parts too in Matlab?
The function interp1 is what you should look into, if you want to do linear interpolation.
n = 100;
phi = linspace(0, 2*pi, n);
H = interp1([data(:,1); 360+data(1,1)]*pi/180, ...
data([1:end 1],3), ...
phi);
Bare in mind that linear interpolation in polar coordinates is something different than linear interpolation of the corresponding cartesian coordinates! (Notice how round the plot becomes for large n.)