Polar plot using polyfit

9 views (last 30 days)
Ramesh Bala
Ramesh Bala on 14 Oct 2020
Answered: Image Analyst on 14 Oct 2020
I'm trying to fit the VALUES to make a polar plot but it seems I couldn't fit one value 1400e2 which the polyfit doesn't take it? what other function should I use to make the polar plot correct so that it contains the values exactly at that degree ?
VGV = [ 2000e2 1800e2 1600e2 1400e2 ]; % [0 45 90 135 ]
angle = (pi/180).* [ 0 45 90 135 ];
alpha = (0:0.1:360);
alpha1 = (pi/180).*(0:0.1:360);
for k=1:1:length(alpha)
p = polyfit(angle,VGV,2);
f = polyval(p,alpha1(1:1:901));
f1 = fliplr(f(1:1:900));
f2 = fliplr(f1);
f3 = fliplr(f2);
Vp = [ f f1 f2 f3];
end
figure;polar(alpha1(1:1:3601),Vp,'o')

Accepted Answer

Image Analyst
Image Analyst on 14 Oct 2020
It seems you want to fit a section between 0 and 135 degrees, then squish that to between 0 and 90, then replicate that around the circle for all 360 degrees. Seems kinds weird, but okay, whatever...
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Create training data.
VGV = [ 2000e2 1800e2 1600e2 1400e2 ]; % [0 45 90 135 ]
angleDegrees = [ 0 45 90 135 ];
angleRadians = (pi/180).* angleDegrees;
% Plot training data
polar(angleRadians, VGV, 'bo')
% Fit a quadratic
coefficients = polyfit(angleRadians, VGV, 1)
% Create 90 more angles to fit between our training angles.
alpha = linspace(min(angleRadians), max(angleRadians), 90);
% Get a fit over 0-135 degrees only.
vFit = polyval(coefficients, alpha);
% Plot fitted data:
hold on;
% Make other quadrants
vFit360 = [vFit, fliplr(vFit), vFit, fliplr(vFit)];
t = linspace(0, 2*pi, length(vFit360));
polar(alpha, vFit, 'r-')
polar(t, vFit360, 'b-')
% figure
% plot(t, vFit360, 'b-');
% hold on;
% plot(alpha, vFit, 'r-', 'LineWidth', 2);
fprintf('Done running %s.m ...\n', mfilename);

More Answers (2)

Alan Stevens
Alan Stevens on 14 Oct 2020
I'm somewhat confused by your question and don't understand the need for all the flip commands! However, does the following meet your needs at all (incidentally, angle vs VGV is a straight line, so you don't need to get polyfit to fit a quadratic):
VGV = [ 2000e2 1800e2 1600e2 1400e2 ]; % [0 45 90 135 ]
angle = (pi/180).* [ 0 45 90 135 ];
alpha = (0:0.1:360);
alpha1 = (pi/180).*(0:0.1:360);
for k=1:1:length(alpha)
p = polyfit(angle,VGV,1);
f = polyval(p,alpha1);
% f1 = fliplr(f(1:1:900));
% f2 = fliplr(f1);
% f3 = fliplr(f2);
%
% Vp = [ f f1 f2 f3];
end
figure;
polarplot(alpha1,f,angle,VGV,'o')
This results in
  1 Comment
Ramesh Bala
Ramesh Bala on 14 Oct 2020
Thank you for your reply.I was using Flip to get the values in all quadrants.I was trying to fit these 0,45,90,135 using a fit and then to get values for other angles based on that that's what Vp was giving.
Now,based on your code how can i get for all 360 degrees?

Sign in to comment.


Image Analyst
Image Analyst on 14 Oct 2020
First of all, polyfit does not need to be inside a loop since angle and VGV don't change inside the loop.
Next, you can fit a line, order 1, instead of a quadratic of order 2 since your VGV is linear.
Next, you have only 2 coefficients for p instead of 900.
% Create training data.
VGV = [ 2000e2 1800e2 1600e2 1400e2 ]; % [0 45 90 135 ]
angle = (pi/180).* [ 0 45 90 135 ];
% Create more angles to fit between our training angles.
alpha = (pi/180).*(0:0.1:360);
% Plot training data
polar(angle, VGV, 'bo')
% Fit a quadratic
coefficients = polyfit(angle, VGV, 1);
% Get a fit
vFit = polyval(coefficients, alpha);
% Plot fitted data:
hold on;
polar(alpha(1:1:3601), vFit, '-')
  3 Comments
Image Analyst
Image Analyst on 14 Oct 2020
Edited: Image Analyst on 14 Oct 2020
If you want 360 interpolated angles instead of 3601 interpolated angles, you'd have to change the way you define alpha to this:
alpha = (pi/180).*(0 : 359); % Exactly 360 angles
If you really want, you can plot blue circles with 'bo' instead of red lines with 'r-'.
If you really want that weird shape you have instead of a spiral, then you'd have to do that flipping craziness, but be aware that alpha no longer means the angle when you go to plot it.
Ramesh Bala
Ramesh Bala on 14 Oct 2020
ah ! so you mean even if I flip this obtained fit ,it wont be giving the proper values (like 1400e2 at 135 and it won't be same at 270 ?)

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!