How do you connect two points such as (1,1) and (2,2) using开发者_如何转开发 a wavy line instead of a straight line in Matlab?
I have looked at the function line([1 2],[1 2]) but it does not provide such functionality.
Thanks!
Like @Nzbuu said, it's hard to know what you want your wavy line to look like. The following function will plot a sinusoid on your current figure between the 2 points, with periods and amplitude specified. If your definition of wavy is different than mine, just change xx
and yy
to describe your wavy line between [0,0]
and [1,1]
.
function wavyline(pt1, pt2)
% first, create a "unit" wavy line segment
xx = [0:.01:1];
reps = 5 % how many periods of sine you want
widthRatio = 0.2 % height of peaks / lenght of line
yy = (widthRatio/2)*sin(reps*xx*2*pi);
% stretch our unit wavy line to be the proper length
dx = pt2(1) - pt1(1);
dy = pt2(2) - pt1(2);
len = sqrt(dx*dx+dy*dy);
xx = len*xx;
% now rotate it
th = atan2(dy, dx);
R = [cos(th), -sin(th); sin(th), cos(th)];
rotatedPts = R * [xx;yy];
% finally, shift it to start at pt1, and plot
shiftedPts = rotatedPts + [pt1(1); pt1(2)] * ones(size(xx));
plot(shiftedPts(1,:), shiftedPts(2,:),'k')
end
There's no built-in functionality in MATLAB to do this. You have to write your own function to do it.
It's hard for someone else to write something generic because it depends on what you think "wavy" means and how "wavy" you want it to be.
It seems like you want a power-series fit - at least, I assume by "wavy" line, you mean something like a quadratic or cubic or etc fit.
That's fine, but to fit a power series sensibly, you basically need one more constraint than the power that you're fitting to.
So, if you only have two points (or one point and a slope), that means you can only fit x^1
, which is of course a linear fit.
EDIT: To answer your question, in the event that you can get more constraints, you probably want to use polyfit or something similar. Googling for "matlab polynomial fit" will probably answer your question better.
精彩评论