1
$\begingroup$

How do you plot the following parametric equation (equation of an ellipse) in the same graph

$$ x = a\cos{t} $$ $$ y = b\sin{t} $$

with varying value of $a$ and $b$ in either maple or MATLAB ?

Many thanks.

$\endgroup$
2
  • $\begingroup$ MaplePrimes is a better place for such questions. $\endgroup$ – user64494 Jun 27 '13 at 14:20
  • $\begingroup$ Thanks .. Didn't know that !! .. $\endgroup$ – Vineet Maheshwari Jun 27 '13 at 17:49
0
$\begingroup$

And in MATLAB:

min_a = 1; max_a = 10;
min_b = .5; max_b = 5;
t = linspace(0,2*pi,101);
cost = cos(t);
sint = sin(t);

N = 21; % Number of distinct values of a, b to plot
a = linspace(min_a,max_a,N);
b = linspace(min_b,max_b,N);

x = kron(a',cost);
y = kron(b',sint);

figure; hold on
for i=1:size(x,1)
    plot(x(i,:),y(:,:))
end

enter image description here


Code comparison for loop versus vectorization techniques:

clear all
clc
time = zeros(5,1);
for k=1:5
    close all
    tic
    min_a = 1; max_a = 10;
    min_b = .5; max_b = 5;
    t = linspace(0,2*pi,101);
    cost = cos(t);
    sint = sin(t);

    N = 21; % Number of distinct values of a, b to plot
    a = linspace(min_a,max_a,N);
    b = linspace(min_b,max_b,N);

    x = kron(a',cost);
    y = kron(b',sint);

    figure(1); hold on
    for i=1:size(x,1)
        plot(x(i,:),y(:,:))
    end
    time(k) = toc;
end
mean(time(2:end))

clear all
time = zeros(5,1);
for k=1:5
    close all
    tic
    min_a = 1; max_a = 10;
    min_b = .5; max_b = 5;
    t = linspace(0,2*pi,101);
    N = 21; % Number of distinct values of a, b to plot
    a = linspace(min_a,max_a,N);
    b = linspace(min_b,max_b,N);

    figure(1); hold on
    for i=1:length(a)
        for j=1:length(b)
            for l=1:length(t)
                x = a(i)*cos(t(l));
                y = b(j)*sin(t(l));
                plot(x,y)
            end
        end
    end

    time(k) = toc;
end
mean(time(2:end))
$\endgroup$
1
$\begingroup$

Here is the command and the plot

plot([[ cos(t), 2*sin(t), t = 0 .. 2*Pi], [2*cos(t), 3*sin(t), t=0 .. 2*Pi],[4*cos(t), sin(t), t = 0 .. 2*Pi]], color = [blue, yellow] );

enter image description here

$\endgroup$
1
  • $\begingroup$ This method works but suppose if i want plot for a=1..10 and b=1..10, this seems to be a very tedious task. I know commands for explicit equations, eg plot({a*sin(t) $ a =-1..1},t=0..2*Pi) Maybe you can manipulate it and tell me the same for parametric equations. Meanwhile i'll also try MaplePrimes as user64494 suggested. $\endgroup$ – Vineet Maheshwari Jun 27 '13 at 18:24
1
$\begingroup$

You may use the following codes in Maple 17 to do your job.

 > with(plots):
 > with(student):
 > for i to 80 do a[i] := i*cos(x) od:
 > for i to 80 do a[i] := \sqrt{i}*sin(x) od:
 > d := seq([a[i], b[i], x = -5 .. 5], i = 1 .. 80);
 > plot({d});

enter image description here

$\endgroup$
0
$\begingroup$

In Matlab

for a=1:3
   for b=1:3
      for t=0:0.01:2*pi
         x=a*cos(t);
         y=b*sin(t);
         hold on
         plot(x,y)
      end
   end
end

enter image description here

$\endgroup$
9
  • $\begingroup$ Never loop over t. cos and sin work just fine on vectors. $\endgroup$ – Emily Jun 27 '13 at 14:51
  • $\begingroup$ Right but takes too much space in memory $\endgroup$ – AnilB Jun 27 '13 at 14:57
  • $\begingroup$ Ummmm.... what? $\endgroup$ – Emily Jun 27 '13 at 14:58
  • $\begingroup$ You're not saving any memory when you hold on and plot a set of single points. That still takes up memory, even when you over-write the variable, because MATLAB creates a separate data structure for the axes that completely contains all data used to generate the plot. $\endgroup$ – Emily Jun 27 '13 at 14:59
  • $\begingroup$ But in your case you have the plot + vectors $\endgroup$ – AnilB Jun 27 '13 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.