How to graph an ellipse on a scatter plot by VBA

Landcruiser87

New Member
Joined
Feb 7, 2008
Messages
16
Helloooo to the masses of fellow coders,

After hours of pouring over the world wide web looking for sample code that might be able to accomplish the title of this post, i've come up empty handed.

What i'm attempting is to create a vba routine that can take a subset of data
Ex:

X Y
29.70 264.00
25.50 269.00
27.90 265.00
37.20 269.00
54.90 262.00
41.10 270.00


And have a vba routine find its centroid, draw a std ellipse, then rotate it 360° to find the minimum area and thus find the angle at which the ellipse best represents the weighting of the data.

I'd like to use matrices and arrays in VBA, but am having trouble finding any examples to work off. So i pose the question to you my friends, Have any of your tried this before? Help this struggling scientist figure out something that seems easy but has thrown up more road blocks than the 405N at rush hour. ( i live in cali )

Thanks !
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Your title is incomplete.
You want to recreate an ellipse with a small subset of the points.
With 4 points, making a rectangle, there is a whole family of ellipse that will incorporate the points.
So how many points are needed?
It would seem at least 5.
And how would you do this?
By solving a system of equations?

There is no way that an ellipse can fit your sample points.
Did you try plotting them on a scatter plot?

Example in parametric form:
Major axis is 2x5=10
Minor axis is 2x2=4
Rotated counterclockwise 45 degrees
In parametric form; allows rotation (Phi in radians); allows offset from origin (Xc and Yc):
Excel Workbook
ABC
1Parametric0.012566
2Phi0.785398
3Xc1
4Yc2
5a5
6b2
7XYt
84.5355345.5355340
94.5174845.5530260.012566
104.4988785.5699570.025133
114.479725.5863240.037699
124.4600125.6021250.050265
134.4397585.6173570.062832
144.4189615.6320170.075398
154.3976245.6461050.087965
164.375755.6596160.100531
174.3533435.672550.113097
184.3304075.6849030.125664
194.3069455.6966750.13823
204.2829615.7078630.150796
214.2584585.7184650.163363
224.2334415.7284810.175929
234.2079135.7379070.188496
244.1818785.7467430.201062
254.1553415.7549880.213628
264.1283065.762640.226195
274.1007775.7696970.238761
284.0727585.7761590.251327
294.0442545.7820250.263894
304.0152695.7872940.27646
...
Cell Formulas
RangeFormula
B1=PI()*2/500
B2=PI()/4

Copy down through Row 508:
Cell Formulas
RangeFormula
A8=B$3+B$5*COS(C8)*COS(B$2)-B$6*SIN(C8)*SIN(B$2)
B8=B$4+B$5*COS(C8)*SIN(B$2)+B$6*SIN(C8)*COS(B$2)

Copy down through Row 508:
Cell C9:
=C8+B$1
Example Ellipse (rotated and not centered on origin):


The first thing you need to do is figure out how you will construct the ellipse from the points.
This can always be converted to VBA Code.

Here is a reference to plotting an ellipse, without rotation of the major axis from the horizontal:
Ellipse in a chart
http://www.ozgrid.com/forum/showthread.php?t=46470
 
Upvote 0
Reconstruction Of Ellipse From Four Points

This is for the simple case where the major axis of the ellipse is horizontal.
It takes only 4 points here to reconstruct the ellipse.
It may be that more than 4 points are needed if the first 4 make a rectangle.

Excel's Solver will solve this, but not consistently.
It depends on the starting conditions.

For the same ellipse as previously, except that Phi = 0, take 4 points:

In Solver:
Set Target Cell = $F$2
The Target Cell should be minimized.
Changing Cells = $F$3:$F$6

Constraints:
$F$5 >= 0
$F$6 >= 0
$G$9 = 1
$G$10 = 1
$G$11 = 1
$G$12 = 1

Cell Formulas
RangeFormula
F2=SUM(G8:G11)

Copy Down Four Cells:
Cell Formulas
RangeFormula
G8=(E8-F$3)^2/F$5^2+(F8-F$4)^2/F$6^2

Before running Solver:

Excel Workbook
ABCDEFG
1Parametric0.012566
2Phi0Sum14.37605
3Xc1Xc2.5
4Yc2Yc0.75
5a5a6
6b2b1
7XYtXYEquation
82.7820593.86865802.7820593.8686589.728237
9-3.960572.2506660.012566-3.960572.2506663.411417
10-2.138460.4430750.025133-2.138460.4430750.691849
111.7511280.0226970.0376991.7511280.0226970.544548
...

After running Solver:
Excel Workbook
ABCDEFG
1Parametric0.012566
2Phi0Sum4
3Xc1Xc1.000001
4Yc2Yc2
5a5a5.000001
6b2b2
7XYtXYEquation
82.7820593.86865802.7820593.8686581
9-3.960572.2506660.012566-3.960572.2506661
10-2.138460.4430750.025133-2.138460.4430751
111.7511280.0226970.0376991.7511280.0226971
...

To get this to work in Excel, at any angle, with VBA Code, and the other things you want it to do, it looks like you have your work cut out for you.
As your search confirms, it is not likely that custom-made solutions are always available.
This has been done, but it may not be readily available, done with the program that you want, or do exactly what you want.

Here is a reference to a Matlab Question and Answer:
Thread Subject: SOLVE 4 NONLINEAR EQUATIONS (HELP PLEASE)
http://www.mathworks.fr/matlabcentral/newsreader/view_thread/280862

Ellipse; 4 points (pink)


 
Upvote 0
JackBean,

Thanks for all the great info on your posts! You're right, i'm trying to draw an ellipse with a minimal amount of points, but i'm not going to restrict myself to only those little points in my graphing. I'm working on the subroutine to build the matrix array as of now and predict an ellipse of at least 70 or 80 pts that would include the smaller points. Your last link to the matlab question i think is going to be the key to save the day.

I'll post back with progress as i'm just trying to figure out the best way to attack the problem. Thanks again for your help,

Andy
 
Upvote 0
This is great stuff!
I used the parametric with rotation form and all works smoothly. But I have an additional question. Anyone an idea on how to determine the focus points of your drawn ellipse?
 
Upvote 0
Hi,

I am also trying to fit a tilted ellipse which also has small no zero Xc,Yc. I have about 30 points. The problem I am facing is the equation:

(X-Xc)^2+(Y-Yc)^2= a^2*cos^2(t)+b^2*(sin^2(t)
=> (X-Xc)^2+(Y-Yc)^ - a^2*cos^2(t) - b^2*(sin^2(t)=0

As the tilt is also to be determined,(phi is to be determined), for fitting purpose I put the parameter t=Phi and got a solution.

As you have already done this, I thought I will ask you.

Thanks
 
Upvote 0

Forum statistics

Threads
1,217,040
Messages
6,134,144
Members
449,861
Latest member
DMJHohl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top