%%I am trying to find the area trapped between two circles and a curve in Matlab, circle one: 10^2= x^2 + y^2 . circle two: 5^2= x^2 + y^2 . curve: y= x^2 . xaxis= [ -10 -5 0 5 10], yaxis [ -10 -5 0 5 10], x and y centre is 0. how can I find the area trapped between the three shapes?
Since you have tagged matlab I assume you are not going for the cleanest solution (analytic, as suggested by #Adriaan). Finding the intersections would also require some analytical work, so I will suggest a method avoiding even that.
Plot the three lines and choose which of the trapped areas you want to find the area of (there are 2 or 4 depending on the definition).
Using Monte Carlo integration you can "throw darts" at your plot.
Use logical statements to see if the dart hits inside or outside the section.
The area is then the percentage of darts inside the section times the area of the section the darts are thrown at.
The more darts you throw the more exact the result will be.
Again, if you just want the exact result do it analytically. For help with that this is not the correct forum.
Related
Explanation of the problem:
I have points with (x,y,z) coordinates at two+ distinct times. For convenience, they can be imagined as irregularly spaced points along the surface of an inverted paraboloid.
There is some minimal thickness to the paraboloid. The paraboloid changes shape slightly as time proceeds (like a balloon inflating) and when it does so, all of the points move.
By substracting the coordinates at time2 - time1, I can get the displacement vectors at each point.
It is important to note (and I suspect this might be the source of the problem) that at the first time point, the x and y coordinates range from 0 to 2000, and the z coordinates are all within a narrower range - say 350 to 450. During the deformation, each point has an x component of displacement, y component, and z component.
The x and y components are small (~50 at most), while the z component is the largest (goes up to 400 near the center, much less near the edges).
Using weighted moving least squares at the location of each point, I am trying to fit the components of displacements to a second degree polynomial surface in terms of the original x,y,z coordinates of the point: eg.
x component of
displacement = ax^2 + bxy + cx + dy^2.. + hz^2 + iz + j
I use the lsqr function in MATLAB,like so, looping through each point for each time interval:
Ux = displacements{k,1}(:,1);
Cx = lsqr((adjust_B_matrix'*W*adjust_B_matrix),(adjust_B_matrix'*W*Ux),1e-7,10000);
W is the weight matrix, and adjust_B_matrix is the matrix of all (x,y,z) coordinates at time 1, shifted so that they're all centered around the point at which I'm trying to fit the function.
What is going wrong?
It's just not working -- once I have the functions, they're re-centered around the actual coordinates of the points.
But once I plot the resulting points (initial pointx + displacementx, initial pointy + displacementy, initial pointz + displacementz) by plugging in the coordinates at time 1 into the now-discovered functions, it just spits out a surface that looks just like the surface at time 1.
What might be going wrong? Things I have tried:
It's not an issue with the code itself- I generated 'fake' data using a grid of points and it worked perfectly. The predicted locations were superimposed with the actual coordinates and I was able to get back the function I started with. But in my trial example, I used x,y,z from 0 to 5, evenly spaced.
Global fitting works (but I need local fitting...).
I tried MATLAB's curve fitting toolbox and just tried to fit one of the displacements to only x and y coordinates, globally. It worked perfectly.
I think I shouldn't have a singular matrix issue because I use a large radius (around 75-80) points in the calculations, somewhat dispersed in 3D space.
Suspicions:
I think it has to do with the uneven distribution of initial (x,y,z) coordinates, but I don't know why or how to fix the issue, or even what method I can use.
If you read this far, thank you so much. Any advice would be greatly appreciated.
Figure for reference:
green = predicted points at time 2. Overlapping mostly with red, the actual coordinates of the points at time 1.
blue is the correct coordinates of points at time 2 (this is where the green ones should be if things were working).
image
Updated link for files:
http://a.tmp.ninja/eWfkNmFZyTFk.zip
Contents - code, sample data (please load the .mat files).
I can't actually access the code you posted, so here's some general suggestions.
It does look like the curve fitting toolbox has tools that do exactly what you are looking for, checkout the bottom of this page: https://www.mathworks.com/help/curvefit/polynomial.html#bt9ykh.
It looks like for whatever your learned function for the displacement is just very small or zero everywhere. I suspect the issue is just a minor typo/error on your part somewhere in your pipeline, possibly translating what you have to work with the fit function will reveal the issue.
This really shouldn't be the issue, but in the future if you had much more unbalanced data you could normalize it all before fitting (x_norm = (x - x_mu)/x_std).
Also, I don't think this is your problem either, but you can check if your matrix is close to singular by checking the condition number using the cord() function. So you could check cond(adjust_B_matrix'Wadjust_B_matrix). Second, If you check the documentation for lsqr there is an option to get a debug return flag, that is worth checking too.
I plot the following figure and I want to decrease the distance between x axis labels for example the distance between 1 and 2 or 2 and 3.
I do not know how can I do that !!!
I found these pages 1 and 2 but I could not know how should I write those codes without getting exceptions. Since, I am not familiar with Matlab environment.
The simple way is resize your figure or change it's position. It would change the distance.
I'd much rather be asking you about Bayesian analysis over this, but unfortunately, I am terrible with MATLAB plots (not due to a lack of effort though).
I have a plot attached. It contains 1739 days of data. I need to do the following three things:
Focus in on the plot so that the y axis limits are -1 and 1 (right now they are -4 and 14). I'm uninterested in any values beyond these limits.
Re-label the y axis from -1 to 1 in .10 increments.
Re-label the x axis so that it starts with 1, ends with 1739, and has enough points in between that it is still readable.
I've studied gca, datetick, ax=gca, etc. but I cannot accomplish any of these tasks without messing up the plot.
I am gracious for any assistance provided.
Changing the axis limits is very simple,
axis([1 1739 -1 1]) %// [xMin xMax yMin yMax]
And for the tick marks, do
set(gca,'XTick',-1:.1:1,'YTick',1:79:1739) %// 2*79 might be better than 79
after you create the plot. You can (should?) replace gca with the handle to the plot (do h=plot(... when you make the plot, then set(h,'XTick',...).
You should also go through Matlab Plotting Basics which goes through all this kind of stuff in good detail.
I have a 7x2 matrix that contains data to plot out a shape. So my script is:
myMatrix = ([3,3;4,2;5,1;9,1;5,1;4,2;3,3]);
plot(myMatrix);
axis([-15 15 -15 15]);
I'm trying to translate it down 3 units and left 4 units. So that my shape will have the points (0,0) as the center. I could move it vertically simply by creating a new variable and defining it as -3+myMatrix. This will translate it down 3 units, but I am unable to move it along the x-axis.
There probably is a simple solution to this but I have been going to the documentation for matlab and googling but I couldn't find anything that will help me with this problem.
Try this:
plot(-3:3,myMatrix-3)
When no x is supplied to plot, the x coordinates are implicitly 1:length(y) when y is a vector or 1:size(y,1) for a matrix. This instead supplies -3:3 as x.
I have set of 3D points.
Points_[x,y,z]% n*3 where n is number of points
I want to fit a plane (it is floor) and check height of plane. I think it is 2D problem .
z=bo+b1x+b2y;
I can't find a link for 2D ransac plane fitting. Can someone please give this link or file.
Secondly, Some softwares (commercial)gives height value of plane. It is mean or some complex value.
Regards,
If you form the following "A" matrix
A = [ones(numel(Points_X),1), Points_X(:), Points_Y(:)];
where the (:) is to give you column vectors (in case they were not to begin with)
Then you can write your equation as the classic linear system of equations:
A*b = Points_Z(:);
where b = [b0; b1; b2] -- a column vector of the parameters you are trying to determine.
This has the canonical solution
b=A\Points_Z(:)
or b=pinv(A)*Points_Z(:)
See help on mldivide and pinv.
You must have 3 or more points which don't all lie on a line. For an overdetermined system like this, pinv and \ will basically produce the same results. If they are nearly colinear, there may be some advantage using .
The 3 parameters in b are basically the height of the plane above the origin, the x slope, and the y slope of the plane. If you think about it, the "height" of a plane IS your z term. You can talk about height above some point (like the origin). Now, if you want height at the center of mass of the sampled points, you would then do
z_mean = [1 mean(Points_X(:) ) mean( Points_Y(:) )] * b
which is probably just equivalent to mean( Points_Z(:) ). For this definition to be meaningful, you would have to ensure that you have a uniformly spaced grid over the region of interest.
There may be other definitions, depending on your application. For instance, if you are trying to find the height at a center of a room, with points sampled along the walls and interior, then replacing mean with median might be more appropriate.