开发者

MATLAB Heart Curve

I am trying to get this in MATLAB, however I am not successful ! Any MATLAB gurus out there ?

alt text http://www.freeimagehosting.net/u开发者_如何转开发ploads/94020b921d.gif

I guess simple commands should work, a program may not be needed.

The error, I am getting is;

>> t = -pi:0.1:pi;
>> r = ((sin(t)*sqrt(cos(t)))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2 ;
??? Error using ==> mtimes
Inner matrix dimensions must agree.

UPDATE

Even this did not work !

>> t = -pi:0.1:pi;
>> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;
>> plot(r,t)
??? Error using ==> plot
Vectors must be the same lengths.


Here's a solution that works:

t = -pi:0.1:pi;
r = ((sin(t).*sqrt(abs(cos(t))))./(sin(t) + (7/5))) - 2*sin(t) + 2 ;
polar(t,r)

EDIT

polar and ezpolar are very limited in the customizeability of the plot. If the OP wants to reproduce the picture in the question, they should convert to cartesian coordinates and use plot, like so

t = -pi:0.1:pi;
r = ((sin(t).*sqrt(abs(cos(t))))./(sin(t) + (7/5))) - 2*sin(t) + 2 ;
[x,y] = pol2cart(t,r);
plot(x,y)


I wonder whether plot is the right command for this. I tried this:

ezpolar('((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2')

and almost got the diagram OP is looking for. I suspect OP might do even better with polar. Trying plot(r,t) gave me a squiggle in (x,y) space and a warning:

Warning: Imaginary parts of complex X and/or Y arguments ignored


Try replacing the * with .* to do component wise multiplication. Also maybe try plot(r,t) instead of plot(s,t).


Try this:

>> t = -pi:0.1:pi;
>> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;

You need to use .* and .^ to tell MATLAB to do componentwise multiplication and exponentiation. Otherwise, MATLAB will try to do a matrix multiplication (for .*) or matrix inversion (for .^(-1)).


If you want to run a complex formula on each item in a matrix, a straightforward approach is to use the arrayfun function.

% Define the formula that is applied to each element
f = @(t) ((sin(t)*sqrt(abs(cos(t))))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2;

t = -pi:0.1:pi;
% Apply the formula
r = arrayfun(f, t);
plot(abs(t), r);

That gives you the idea, though you might have to fix the formula. For example, shouldn't that be sqrt(abs(cos(t)))?


x1=linspace(-1,0,50);
y1=-x1+sqrt(3-3*x1.^2);
y2=-x1-sqrt(3-3*x1.^2);
plot(x1,y1,'r');hold on;
plot(-x1,y1,'r');
title ('MY HEART');
plot(-x1,y2,'r');
plot(x1,y2,'r');hold off;


theta = 0:.1:2*pi;

r = (sin(theta).*sqrt(abs(cos(theta))))./(sin(theta) + (7/5))-2.*sin(theta)+2;

polar(theta, r,'-r');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜