MATLAB Answers

0

Help I can't get my code to execute

Asked by Blessing Makwera on 15 Sep 2019 at 21:00
Latest activity Commented on by Blessing Makwera on 16 Sep 2019 at 3:55
clear
clc
r = 0.12; % in m
c = 0.250; % in m
t = linspace(0,0.12); % time equally spaced for 1 rev
thd = (50/3)*pi; % d(theta)/dt in rad/s
theta = thd*t; % theta as a product of time and d(theta)/dt
d1 = r*cos(theta);
h = r*sin(theta);
d2 = sqrt((c^2)-(h.^2)); % d2 and how its relates to c and h
x = d1 + d2; % The position (x)is a sum of d1 and d2
% Position vs time subplot
subplot(3,1,1), plot(t,x)
title('Problem #1 Answer'), ylabel('Position (m)')
% xd = d(x)/dt
numer = ((r^2) * thd * sin(2*theta)); % breaking down the equation.
denom = ((c^2)-(r^2)*(theta));
xd = (-1)*(r)*(thd)*sin(theta)-(numer/(2*sqrt(denom)));
% Do => Velocity (xd) vs time subplot
subplot(3,1,2),plot(t,xd), ylim([-8 8]) % subplot for velocity vs time
ylabel('Velocity (m/s)')
% Do => Acceleration (xdd) vs time
% Breakdown the eqn into small parts
numer1 = ((cos(2*theta))*4*(r^2)*(thd^2)) * ((c^2) - (r^2)*((sin(theta)).^2));
numer2 = ((r^2) * (thd) * (sin(2*theta)));
denom1 = (c^2) - (r^2)*((sin(theta))^2);
% combine the (xdd) eqns
xdd = (-1)*((thd)^2)*cos(theta) - (numer1 + (numer2)^2)/(4 * (denom1)^(3/2));
subplot(3,1,3), plot(t,xdd)
%% And this is the the error I get
"Warning: Imaginary parts of complex X and/or Y arguments ignored
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second
matrix. To perform elementwise multiplication, use '.*'.
Error in Makwera_Blessing_HW02 (line 39)
numer1 = ((cos(2*theta))*4*(r^2)*(thd^2)) * ((c^2) - (r^2)*((sin(theta)).^2));

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 15 Sep 2019 at 21:22
 Accepted Answer

xd = (-1)*(r)*(thd)*sin(theta)-(numer/(2*sqrt(denom)));
where
denom = ((c^2)-(r^2)*(theta));
but denom can be negative, and sqrt() of a negative value will be complex valued.
Looking at
numer = ((r^2) * thd * sin(2*theta)); % breaking down the equation.
I would tend to suggest that you should not be multiplying by theta itself, but instead by a trig function of theta.
numer1 = ((cos(2*theta))*4*(r^2)*(thd^2)) * ((c^2) - (r^2)*((sin(theta)).^2));
theta is a 1 x 100 vector, so the left side of the * operation cos(2*theta) is a 1 x 100 vector. The right side of the * operation involves sin(theta) and theta is a 1 x 100 vector, so the right side of the * operation is a 1 x 100 vector.
You therefore have (1 x 100 vector) * (1 x 100 vector) . But * is the algebraic matrix multiplication ("inner product") operator, for which the second dimension of the left operand must be the same as the first dimension of the right operand.
If you are looking for a 100 x 100 result, you will need to transpose() or ctranspose() the left side, so you have (100 x 1) * (1 x 100) giving 100 x 100. If you are looking for a 1 x 1 result, you will need to transpose() or ctranspose() the right side, so you have (1 x 100) * (100 x 1) giving 1 x 1. If you are looking for a 1 x 100 result then * is the wrong operation to use; perhaps you should be using the .* operation.

  1 Comment

Thanks very much. This saved me a lot of stress. I am new to matlab and learning a lot.

Sign in to comment.