How to calculate integral of JFreeChart functions - jfreechart

I have 2 XYSeries (call them f1 and f2) in XYSeriesCollection and I want to calculate integral of (f1 - f2). How can I do that? I don't know formula for f1 and f2, I just have some points to draw plots.

Related

How can I convert from cylindrical to cartesian coordinates in 3D for plotting in Matlab?

I am trying to define a function in 3D cylindrical coorindates in Matlab, and then to convert it to 3D cartesian for plotting purposes.
For example, if my function depends only on the radial coordinate r (let's say linearly for simplicity), I can plot a 3D isosurface at the value f = 70 like the following:
x = linspace(-10,10,100);
y = linspace(-10,10,100);
z = linspace(-2,2,100);
[X,Y,Z] = meshgrid(x,y,z);
R = sqrt( X.^2 + Y.^2 ); % Calculate the radius for each [x,y] coordinate pair
f = 100*R; % Calculate the function f, dependent only on the radius
isosurface(X,Y,Z,f,70);
However, since the function depends only on r, I should be able to instead define a vector for the r coordinate only, and calculate f based on that instead:
r = 0:0.1:1 ; % define radial coordinate
f = 100*r ; % make the function linearly dependent on radial coorindate for simplicity
My aim is now to plot an isosurface based only on knowing r and f. I would like to specify a vector of z and theta coordinates, maybe like this
z = linspace(-2,2,100);
theta = linspace(0,2*pi,50);
and use these together with r to generate [x,y,z] coordinates. In addition, f now is a 1D array instead of a 3D array like the first example, so somehow f needs to be reshaped?
The reason for this is because I am trying to do manipulations on f inside a loop many times, and with the first case this results in many operations over a 3D matrix, which is too slow. I would like to instead perform operations on the 1D vector, and generate 3D version only for plotting.
I hope my question is clear. I don't think the pol2cart function does what I want, but I could be wrong.
It is a rather dificult task to represent 4D data. You mention you want to visualize 4D data with the isosurface function. Another alternative is to represent the 4th variable using a color scale, with the slice function.
The problem with these functions is that they work for the cartesian coordinates [X,Y,Z].
What you have to do is to interpolate your data to your domain in cartesian coordinates, before using the functions to represent these 4D data, as in here or here.
Essentially, what you end up with is:
% generate mesh in cylindrical coordinates
theta = linspace(0,2*pi,50);
r = 0:0.1:1;
z = linspace(-2,2,100);
[Theta,R,Z] = meshgrid(theta,r,z);
% Evaluate function in cylindrical coordinates
f = 100*r; % f is function only of radial coordinate
f = f'; % make sure f is column vector
f = repmat(f,1,length(theta),length(z)); % f must be 3D matrix to plot surface
% transform to cartesian coordinates
[X,Y,Z] = pol2cart(Theta,R,Z)
[xg, yg, zg] = meshgrid(linspace(-1,1,20),linspace(-1,1,20),linspace(-2.1,2.1,20));
Px = squeeze(X);
Py = squeeze(Y);
Pz = squeeze(Z);
v = squeeze(f);
F = scatteredInterpolant([Px(:) Py(:) Pz(:)],v(:),'linear','nearest');
f_interp = F(xg,yg,zg);
And now you can use whatever function you want to visualize the 4D data:
isosurface(xg,yg,zg,f_interp,70);
You can interpolate your data with either the scatteredinterpolant function or the griddata function, whatever fits best to you.

Find 3D coordinates of end point of a vector given the distance and the angle

How can I find the coordinates of P ? I've seen other posts in 2D and also in 3D but they say that I need 3 angles for 3D and some say I only need two but I dont understand which ones, I suck at math.
All See this image, I have is those two angles and the distance between B and P
To define a vector in 3D, given its length, you need 2 angles. These 3 coordinates (length + 2 angles) are named "spherical coordinates". There are 3 conventions for defining such angles. The most common one is the radius-elevation-azimuth. In this convention, expression of the cartesian coordinates of the vector, given the radius, elevation angle and azimuth angle are:
x = radius * sin(elevation) * cos(azimuth)
y = radius * sin(elevation) * sin(azimuth)
z = radius * cos(elevation)

Efficient algorithm to determine the shape from the inputted four points

I am trying to solve Determine the shape problem on Uva. From what I can get after reading the question it that it is an Ad Hoc geometry problem in which we have to use some geometry theorem to determine what shape the four points we take as input form on a 2D plane. After spending many hours I still cannot think of any efficient algorithm which can solve the problem efficiently in the given time limit.I tried using distance formula and slopes but was not of much help.Please suggest some good algorithm or theorem I can use to solve this problem.
My first thought was the following:
Determine the lengths of each side. Use the formula sqrt((a - x)^2 + (b - y)^2).
Determine each of the angles.
Determine which shape:
If all angles 90
If all sides equal, return SQUARE
Else, return RECTANGLE
Else if angle 1 = 3 and angle 2 = 4 equal
If all sides equal, return RHOMBUS
Else, return Parallelogram
Else if angle 1 + 2 == 180, or angle 3 + 4 == 180, return TRAPEZIUM
Else, return ORDINARY QUADRILATERAL
But consider the logic followed here; they've used vector math, and simply calculate whether something is a right angle or whether two lines are parallel, rather than calculating all angles.
Prepare some functions to calculate whether two lines are parallel, and whether an angle is a right angle (using vector mathematics).
Sort the points so they're in the correct rotational order (the actual order is ambiguous for concave shapes, but it shouldn't matter, should still return ORDINARY QUAD).
Determine the lengths of each line.
Determine the shape, given the lengths and whether lines are parallel or angles right.
Firstly, different shapes have the following relationship:
Ordinary Quadrilateral --(with one pair of parallel sides)--> Trapezium
Trapezium --(with additional pair of parallel sides)--> Parallelogram
Parallelogram
--(with four equal straight lines)--> Rhombus--(with four 90 degree angles)-->square
|--(with four 90 degree angles)--> Rectangle --(with four equal lines)--> square
Given four points, A, B, C, D, take random one(say A), and we need to calculate statistics(angle and length) of following four pairs of points(not all), including
(1) AB and CD, A1, L1
(2) AC and BD, A2, L2
(3) AB and AC, A3, L3
(4) AB and AD, A4, L4
And then I think the trick comes to how to organize the branches so that we could have minimal computation and code path. My proposal is listed as follows:
A3 = getAngle(AB, AC)
A4 = getAngle(AB, AD)
if A3 > A4
we know AD is the diagonal line, then use A, B, C to calculate
else
we know AC is the diagonal line, then use A, B, D to calculate
# following suppose we use A, B, C to do the calculation, we could easily do the A, B, D thing if define new variables
L3 = LengthEqual(AB, AC)
A1 = getAngle(AB, AD)
if A3 == 90 && A1 == 0
if L3 == True
Square
else
Rectangle
else
A2 = getAngle(AC, BD)
L2 = LengthEqual(AC, BD)
if A2 == 0 && A1 == 0
if L2 == True
Rhombus
else
Parallelogram
else if A2 == 0 || A1 == 0
Trapezium
else
Ordinary Quadrilateral
In this means, we could achieve relatively less computation to wisely branch to the results we want. Hope this helps.

What exactly is the output quaternion of slerp?

I'm trying to implement SLERP (described by Ken Shoemake in "Animating Rotation with Quaternion Curves)
I've read up on the topic on wikipedia (topic: quaternions, 1 and 2) and other sites and also searched stackoverflow about this problem. It seems like I understand the theory behind it, but oversee one small detail. I will use w for the scalar value of the quaternion
So initially I have two 3D vectors. Each vector has a representation in two coordinate systems (C and C'). My goal is to find a third representation of these vectors in the system "halfway" the initial two.
So what I do is I find the rotation matrix, which transform the vectors from C to C', which seems to work out quite fine.
My next step is to transform this matrix into a quaternion, which also works.
Now my issue is with the formula of slerp, which is:
slerp(q1, q2; u) = ((sin(1-u) * t)/ (sin t)) * q1 + (sin(ut)/sin t) * q2
(sorry can't upload images yet for a better representation: see source 1)
so I guess here u = 0.5, q1 is the vector I would like to rotate (with w=0) and q2 equals the quaternion I calculated previously. Theta is calculated from the dotproduct of the normalized vector and the (already) normalized quaternion.
So what I expect is that I get back a vector, rotated either from C to the third coordinate system or from C' to the third coordinate system.
My issue now is, that I don't see, how I will get a vector and not a quaternion. Meaning, how is it possible, that I will get a quaternion with (w=0), as by simply multiplying q2 with this factor won't set w to 0. Or is it something else I will get from this function?
What am I overseeing here?
Thanks for your help!
Seems like I figured it out. For someone with the same understanding problem:
slerp simply interpolates between two orientations, meaning between two actual rotations. So in my case, q1 is the quaternion corresponding to the identity matrix (so [1, 0, 0, 0]). q2 is the rotation. theta is still 0.5.
With the quaternion I get from this, I have to calculate the rotation with q^-1 v q. Where v is my vector I want to rotate. This can be calculated using the Hamilton product.

Precise angle calculation

I am working on a project in which i have to rotate a tyre based on the value from the position sensor. Sensor provides values from 261(at -90 degree) to 395(at +90 degree). I am using TransformGroup so almost at 327 value from sensor the tyre should be straight and erect.
When value is 395 then tyre will totally lie along X axis similar is case for value 261. I am able to move the tyre but i am unable to calculate precise angle.
My way of calculation is like that i calculate the number of values btween the default value or zero value(327) for a specific angle(say 3). so i get number of values for that angle. Then i divide this number from the angle(3 degree). So i get angle for one value.
float tempangle = value from sensor-zero value which is 327;
tempangle = Math.Abs(tempangle);
tempangle /= float.Parse(angle for reference say 3 degree);
tempangle = 1 / tempangle;
Here i put some body of known angle under sensor(which have an agle of degree). When i want to find some angle for some value i just multiply this angle with the number of values between that value and default value(327). So i get the angle to rotate the body.
suppose i have got angle after calculation
angle for one sensor value =1.14286286
Suppose i calculate angle by calculator like
1.14286286 *2=2.2857
1.14286286 *3=3.42858
1.14286286 *4=4.57145
1.14286286 *5=5.714314
1.14286286 *6=6.85717
As you can see that angle is skipping some decimal values. Is there any way to co op with it?
thanks
No idea if I understand your problem, but a method that calcaluates an angle between -90 and 90 degree from an input value in the range 261 to 395 would simply look like this:
private static double SensorValueToAngle(double value)
{
const double sensorMin = 261;
const double sensorMax = 395;
const double angleMin = -90;
const double angleMax = 90;
return angleMin + (angleMax - angleMin)
* (value - sensorMin) / (sensorMax - sensorMin);
}

Resources