Python Forum

Full Version: Arrays in MATLAB and PYTHON
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I am a newbie at Python. I want to "translate" my code in MATLAB into Python but meet a lot of problems.
For example, this is my code in MATLAB: To create a circle
alpha = 0:2*pi/10000:2*pi;
R = 100000;
for i = 1:length(alpha)
x(i) = R*cos(alpha(i))
y(i) = R*sin(alpha(i))
end
plot(x,y,'r')
Can anyone else help me do it in Python please? Thank you very much.
Show what have you tried and what "problems" you meet.
Post your code in python tags, any traceback - in full, in error tags
import math
import numpy as np
from matplotlib import pyplot as plt
import pandas as pd

def fx(R, alpha):
    x = R*math.cos(alpha)
    y = R*math.sin(alpha)
    return x,y

alpha = np.linspace(0, math.pi*2, num = 10000)
Rmax = 100000
for i in range(alpha):
    (x[i], y[i]) = fx(Rmax, alpha[i])
Error:
TypeError Traceback (most recent call last) <ipython-input-14-8444e88ffbda> in <module> 2 alpha = np.linspace(0, math.pi*2, num = 10000) 3 Rmax = 100000 ----> 4 for i in range(alpha): 5 (x[i], y[i]) = fx(Rmax, alpha[i]) 6 TypeError: only integer scalar arrays can be converted to a scalar index
Here is my code and error.