Reputation: 83
I have the following code:
# package for ploting functions
using Plots
# use GR
gr()
# nb points to plot
nbPts = 22
# define polar coordinates of a 30 degree (pi/6) rotation
sine = sin(pi/6)
cosine = cos(pi/6)
# scale factor
scale_factor = 0.9
#---------------------------------------
# 1. PLOT POINTS USING ROTATION MATRIX
#---------------------------------------
# define Rotation matrix ( angle = pi/6, center = (0, 0) )
R = zeros(Float64, 2, 2)
R[1,1] = R[2,2]= cosine
R[1,2] = -sine
R[2,1] = sine
# Scale matrix
### ... <-- EXERCISE 4(c): define a uniform scaling matrix (use scale_factor)
# arrays of points coords
X_mat = zeros(nbPts)
Y_mat= zeros(nbPts)
# first Point (1,0)
X_mat[1] = 1.0
Y_mat[1] = 0.0
for i in 2:nbPts
prevPoint = [X_mat[i-1], Y_mat[i-1]]
#apply rotation to previous point to obtain new point
newPoint = R * prevPoint
### ... <-- EXERCISE 4(c): apply scaling matrix
X_mat[i] = newPoint[1]
Y_mat[i] = newPoint[2]
end
# plot points in blue
plt1 = scatter(X_mat, Y_mat, color=:blue, xlim = (-1.1, 1.1), ylim = (-1.1, 1.1), label=false, title="Rotation using matrices" );
#---------------------------------------
# 2. PLOT POINTS USING COMPLEX NUMBERS
#---------------------------------------
function ComplexProduct(z, w)
(((z[1]*w[1])+(z[2]*w[2])),((z[1]*w[2])+(z[2]*w[1])))
### ... <-- EXERCISE 4(b): implement complex product z * w
end
# first point: z = 1 + 0 * i
Z = ( 1.0, 0.0 )
# second point: w = cosine( pi/6) + sine( pi/6) * i
W = ( cosine, sine )
### ... <-- EXERCISE 4(c): apply scale_factor to W
# arrays of points coords
X_comp = zeros(nbPts)
Y_comp = zeros(nbPts)
# first Point (1,0)
X_comp[1] = Z[1]
Y_comp[1] = Z[2]
for i in 2:nbPts
prevPoint = (X_comp[i-1], Y_comp[i-1])
newPoint = ComplexProduct(prevPoint[1], prevPoint[2]) ### <-- EXERCISE 4(b): compute newPoint by applying rotation to prevPoint (use complex product)
X_comp[i] = newPoint[1]
Y_comp[i] = newPoint[2]
end
# plot points in red
plt2 = scatter(X_comp, Y_comp, color=:red, xlim = (-1.1, 1.1), ylim = (-1.1, 1.1), label=false, title="Rotation using complex numbers" );
# arrange and display
display( plot( plt1, plt2, layout = (1, 2), size=(600*2, 600) ))
The Error:
The Thing I want: I have to implement a product of complex numbers and this should be used to calculate the rotation with complex numbers.
Should look like that:
What do I have to change so that the BoundsError is fixed? Don't know what exactly i do wrong because of the poorly information i get from this error log.
Greetings and thanks for the help.
Upvotes: 1
Views: 47
Reputation: 83
Fixed the bug by changing the following code:
newPoint = ComplexProduct(prevPoint, W)
in line 92
Upvotes: 0
Reputation: 42194
prevPoint[1]
is a scalar while your function ComplexProduct
expects something that has 2
elements. Perhaps you wanted to pass prevPoint
instead of prevPoint[1]
?
BTW you use incorrect naming pattern. CamelNaming is discouraged for Julia.
Your variable should be named prev_point
and your function should be named complex_product
.
Upvotes: 2