MATLAB Answers

How to plot curve instead of straight line in MATLAB?

13 views (last 30 days)
Ayan Atal
Ayan Atal on 27 Jul 2021
Edited: Walter Roberson on 29 Aug 2021 at 23:11
How do i make the yellow line curve instead of st. Line
  2 Comments
dpb
dpb on 27 Jul 2021
To plot a curved line instead of the straight line between the two endpoints of any section, you'll have to have a way to provide the x,y coordinates of the intermediate points, not just the end points.
One will still get a series of straight lines between the supplied points so you must generate enough additional points between x=5 and x=6 to form what appears to be a smooth curve.
Alternatively, given just one additional point between the endpoints that is not on the straight line, one could use one of the interpolating or smoothing functions to fill in a smooth shape for you.

Sign in to comment.

Accepted Answer

dpb
dpb on 27 Jul 2021
A demo built around a rough approximation of your figure...
figure
% approximate the quadrilateral above
plot([5 5],[250 380],'r-',[5 6],[380 990],'y-',[6 6],[990 500],'m-',[6 5],[500 250],'g')
xlim([4.5 6.2]),ylim([260 999])
hAx=gca; hAx.Color=0.9*[1 1 1]; % yellow didn't show up well on my monitor
hold on
pause(0.5)
% add one point in between...
hL1=plot([5 5.5 6],[380 1.25*mean([380 990]) 990],'y*-');
pause(0.5)
% add one point in between each of those...
L2=[380 1.25*mean([380 mean([380 990])]) 1.25*mean([380 990]) 1.15*mean([mean([380 990]) 990]) 990];
X2=[5 5.25 5.5 5.75 6];
hL2=plot(X2,L2,'y*-');
pause(0.5)
% interpolate between last points with 100 points and spline...
xH=linspace(5,6);
yH=interp1(X2,L2,xH,'spline');
hLS=plot(xH,yH,'-','Color','#EDB120');
If you run this you'll see a figure build with the first quadrilateral then lines added with 1 and 3 intermediate points, then what appears to be a smooth line owing to a hundred points between [5 6].
If one were to blow the axes up to be able to see the range of only a 0.01 dX and each y value plotted, then plot the range of 3 of those, one could see that the line segments between those points even are still straight lines.

More Answers (1)

Walter Roberson
Walter Roberson on 27 Jul 2021
Edited: Walter Roberson on 29 Aug 2021 at 23:11
The MATLAB routines for plotting curves instead of straight lines are:
  • viscircles() (Image Processing Toolbox)
  • rectangle() (basic MATLAB) -- despite its name, you can create ovals and circles from it
  • scatter() or plot() with markershape 'o' and large marker size
The other routines all plot straight lines (and the above may internally create straight lines.)
It is very unlikely that your display can plot actual curves. If it were the early 1980s then you might have had a Tektronix 4010 or 4014 which could plot curves. Even now, some air traffic controllers and military radar sites apparenlty use specialized displays that have an seperate overlay that can draw curves. If you are displaying onto a 1960's CRT, then Yes, you just might be able to draw curves.
But if you are using any display since the early 1980s and you are not using a very very expensive radar display, then chances are very high that you are using a display that is entirely pixelized, individually addressible dots with fixed position.
The trick is that if you plot a lot of adjacent small straight lines, then it can look like a curve, especially if anti-aliasing is on.
... But to do that, you have to pass MATLAB data that is sufficiently dense that when it is represented as pixels, it looks curved.
If you do not have the extra points yet, but you have a model of how the curve should look, then polyfit() with polyval(), or spline() with ppval(), or interp1() with various options, can help create intermediate points.

Community Treasure Hunt

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

Start Hunting!