Fitting Curve To Polarplot Data
Show older comments
Hi all
I want to create fitting curve to my data i ploted in a polar plot. I tried to create fitting curve in matlab-cftool but it did not worked. someone can help how can i create fitting curve in apolar plot data?
Thank you
Answers (1)
William Rose
on 17 Dec 2023
0 votes
As the name suggests, the error function for lsqcurvefit is the sum of squared differences between measured and predicted. For a polar plot, you might consider the theta values to be known, and the r values to be measured and predicted, or vice versa. In eithr case, the use of lsqcurvefit() is straightforward.
If r and theta are both functions of a third variable, such as frequency, then you probably want to define the error function to be the distance on the Cartesian plane between the measured and predictred points. This is the standard approach when fitting frequency response functions.
Provide example data and a candidate function for fitting, if you want more detailed suggestions.
Good luck!
5 Comments
William Rose
on 17 Dec 2023
Edited: William Rose
on 17 Dec 2023
[edit: fix typos]
Here is a simple example of how to use lsqcurvefit to fit a polar data set. The polar function in this example is a cardioid:
The function above has parameters a and ϕ. a sets the radial scale of the curve. ϕ (
) sets the angular offset of the cardioid (i.e. the angle where the cusp is).
We will assume the theta values are given, and the r values are measured, with some measurement error. Our goal is to find a cardioid function that gives the best fit to the data. In other words, we will seek the a and ϕ that give the best fit.
Define a cardioid function with parameter vector p=[a,ϕ]
fun = @(p,xdata)p(1)*(1-cos(xdata-p(2)));
Generate data for fitting
theta=0:pi/12:2*pi; % theta values
pTrue=[2,pi/4]; % parameters a, phi
rTheo=fun(pTrue,theta); % theoretical r values
rData=rTheo+0.4*randn(size(rTheo)); % add random noise to r values
Plot the data
polarplot(theta,rTheo,'--r',theta,rData,'b*');
grid on; legend('Theo','Data'); hold on
Fit the data
p0=[1,0]; % initial guess for p=[a,phi]
p = lsqcurvefit(fun,p0,theta,rData); % estimate [a,phi]
rFit=fun(p,theta); % compute r using best-fit values for [a,phi]
polarplot(theta,rFit,'-b.') % plot the best-fit curve
legend('Theo','Data','Best Fit')
fprintf('Best fit values: a=%.3f, phi=%.3f.\n',p)
This shows how you can fit polar data with lsqcurvefit().
israel cohen
on 18 Dec 2023
You are welcome.
I used the cardioid function to demonstrate the idea of fitting a set of polar data. I did not expect or intend that a cardioid would actually be useful for your data. The plot of your data confirms that a cardioid is not a good fit. I recommend that you write a function, with adjustable parameters, to fit the your data,. The function should be based on what you know about the system that generated the data.
You call the angle data "az". I assume it is azimuth. Azimuth is usually measured from 0 at the top (north), increasing clockwise, like a compass. Therefore I have used polarplot options to display the azimuth values like a compass.
fp = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 3.75, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3.75, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8];
az = [20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 290, 295, 300, 305, 310, 315, 320, 325, 330, 335, 340];
polarplot(deg2rad(az), fp, 'bo','MarkerFaceColor','b')
title("Polar Data");
ax=gca; ax.ThetaZeroLocation='top'; ax.ThetaDir='clockwise';
The plot shows that a cardioid will not be a good function to fit your data. A simple model with two adjustable parameters that would work pretty well is r=(r1 when 90<=az<270, r2 otherwise). You can use lsqcurvefit() to find the values of r1 and r2 that give the best fit, as shown in my earlier posting.
israel cohen
on 18 Dec 2023
William Rose
on 18 Dec 2023
YOu're welcome, @israel cohen
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
