user366312
user366312

Reputation: 16998

Epitrocoid in Python is not giving proper plot

I wrote the following code in C#:

using System;
using System.Drawing;
using ZedGraph;

namespace _1_12_Epitrocoid
{
    class Epitrocoid
    {
        double A = 1.0;
        double a = 0.4;
        double λ = 1.4;

        public double X(double ϕ)
        {
            return (A + a) * Math.Cos(ϕ) - λ * a * Math.Cos(((A + a) / a) * ϕ);
        }

        public double Y(double ϕ)
        {
            return (A + a) * Math.Sin(ϕ) - λ * a * Math.Sin(((A + a) / a) * ϕ);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Epitrocoid e = new Epitrocoid();

            PointPairList list = new PointPairList();

            for (double ϕ = 0; ϕ < 10; ϕ += 0.01)
            {
                double x = e.X(ϕ);
                double y = e.Y(ϕ);

                list.Add(x, y);
            }

            PlotForm f = new PlotForm("Epitrocoid");
            f.Add(list, "Epitrocoid", Color.Black);
            f.AxisChange();
            f.ShowDialog();

            Console.ReadLine();
        }
    }
}

enter image description here

I converted this source code into Python like the following:

import math
import matplotlib.pyplot as plt 

A = 1.0;
a = 0.4;
λ = 1.4;

def X(ϕ):
    return (A + a) * math.cos(ϕ) - λ * a * math.cos(((A + a) / a) * ϕ);

def Y(ϕ):
    return (A + a) * math.sin(ϕ) - λ * a * math.sin(((A + a) / a) * ϕ);


x_list = []
y_list = []


for i in range(0, 1001, 1):
    ϕ = i / 1000.0
    x_list.append(X(ϕ))
    y_list.append(Y(ϕ))

print(len(x_list))
print(len(y_list))

plt.plot(x_list, y_list) 

enter image description here

Can someone tell me what thing is going wrong here?

Upvotes: 0

Views: 39

Answers (1)

Paul H
Paul H

Reputation: 68186

You didn't construct your iterator the same in Python (up to phi = 1) as you did in C# (up to phi = 10).

Using the right phi gets you there with Python. Also, using numpy simplifies things quite a bit.

import numpy
from matplotlib import pyplot

A = 1.0;
a = 0.4;
λ = 1.4;

def X(ϕ):
    return (A + a) * numpy.cos(ϕ) - λ * a * numpy.cos(((A + a) / a) * ϕ);

def Y(ϕ):
    return (A + a) * numpy.sin(ϕ) - λ * a * numpy.sin(((A + a) / a) * ϕ);


ϕ = numpy.arange(0, 10, 0.01)
x = X(ϕ)
y = Y(ϕ)

fig, ax = pyplot.subplots()
ax.plot(x, y)
ax.set_aspect('equal')

enter image description here

Upvotes: 1

Related Questions