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.
a source to share
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
a source to share
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)
a source to share
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)
).
a source to share
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)))?
a source to share