tags:

views:

177

answers:

5

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

alt text

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.
+3  A: 

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

Andreas Brinck
+2  A: 

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)).

Martin B
@Martin B: Thanks, silly mistake on my part ....but even that did not help ! :(
Arkapravo
+1  A: 

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)))?

Andrew Shepherd
+2  A: 

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
High Performance Mark
The imaginary part is caused by sqrt(cos(t)). cos(t) can be negative.
Andrew Shepherd
I tried ezpolar('((sin(t)*sqrt(abs(cos(t))))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2'). It matches perfectly.
Andrew Shepherd
@Andrew Shepherd : Many Thanks ! ..... I bet you are a MATLAB Guru ! :)
Arkapravo
@High Performance Mark : Answer accepted, you suggested ezpolar before anyone else !
Arkapravo
+3  A: 

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)
Jonas
@Jonas : Very nice, too bad I can select only one right answer :)
Arkapravo
@Jonas : This is a generic way of plotting such curves, much appreciated !
Arkapravo

related questions