MATLAB Heart Curve

I am trying to get this in MATLAB but I am not successful! Any MATLAB gurus?

alt text http://www.freeimagehosting.net/uploads/94020b921d.gif

I think simple commands should work, the program might not be needed.

The error I am getting:

>> 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 that didn't 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.

      

+2


a source to share


7 replies


I wonder if is the plot

right command for this. I've tried this:

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

      



and almost got the diagram the OP is looking for. I suspect the OP can do even better with polar

. The attempt plot(r,t)

gave me a throwaway in space (x, y) and a warning:

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

      

+3


a source


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 are ezpolar

very limited in schedule customization. If the OP wants to reproduce the image in the question, they have to convert to Cartesian coordinates and use the graph as if

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)

      

+5


a source


Try replacing *

with .*

to do component multiplication. Also try plot(r,t)

instead plot(s,t)

.

+3


a source


Try the following:

>> 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 about componentwise multiplication and exposure. Otherwise, MATLAB will try to perform matrix multiplication (for .*

) or matrix inversion (for .^(-1)

).

+2


a source


If you want to run a complex formula on each element in a matrix, a simple 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);

      

This gives you an idea, although you may need to correct the formula. For example, shouldn't there be sqrt ( abs (cos (t)))?

+1


a source


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;

      

+1


a source


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


a source







All Articles