Can you explain how to code for homework 2 #3b. How do I write a code for a changing alpha1 and alpha2 and get qe = [xe, ye] out?
In problem 2a, you were able to figure out qe based on alpha1 and alpha2. In 3b, you just need to generalize this such that you just update the alphas and the qe is automatically computed for you. From 3a, you should have the equation that updates each alpha as a function of t. So essentially you need to plug in these alphas into your equation for calculating xe and ye and then graph each versus time. Does this help? Let me know if it's still unclear.
I no longer think I have done 3a correct. I think I ploted alphadot1(xdot1) and alphadot2 (xdot2). function xdot = f(t, x) xdot = zeros(2,1); xdot(1) = .5.*cos(t); xdot(2) = 1/3; end How do I use the code stubs provided to plot alpha1 and alpha2?
What you pasted there looks right...you just need to integrate it just like you did in HW1 using ode45. You could also just integrate it yourself. alpha1 would just be the integral of .5*cos(t) and alpha2 is the integral of 1/3. Keep in mind that both are subject to the initial conditions. That should give you alpha1 and alpha2 as functions of t and you should be able to graph them using the same technique you used in homework 1.
I see now that the code I above gives me alpha1 and alpha2. I need to write code to calculate qe. Something like xe = cos(alpha1) + 0.5*cos(alpha1 + alpha2); ye = sin(alpha1) + 0.5*sin(alpha1 + alpha2); but I am not clear as how I pass alpha1 and alpha2 to a function get xe and ye out and plot xe and ye versus time. Two vectors into a function (alpha1, alpha2) three vectors out (xe, ye, time) and have xe and ye connected to time.
You will have to define the time vector to calculate the alpha1 and alpha2. It doesn't need to be returned by the function because you already have it. All you need to do is pass alpha1 and alpha2 into the function and get xe and ye. Then plot each against the time vector which you already have.
I think I follow. I will try it and get back to you within 15 min. Thanks.
Sounds good. Let me know
Something is going wrong. case 'a' tspan = [0 , 40]; x0 = [pi/4 0]; [t, x] = ode45( @hw2_3a , tspan , x0 ); case 'b' [qe] = fkin2R(x(:,1), x(:,2)); function fkin2R = f(qe) fkin2R(1) = cos(x(1)) + .5.*cos(x(1) + x(2)); fkin2R(2) = sin(x(1)) + .5.*sin(x(1) + x(2)); qe = [fkin2R(1) fkin2R(2)]; end I am getting an error message 'Index exceeds matrix dimensions.'. fkin2R(1) is xe and fkin2R(2) is ye x(:,1) is alpha1 and x(:,2) is alpha2 I want to get qe as a 2 column vector but it is only coming out as a 1 column vector.
For one thing you defined your function as having one input and you gave it 2 inputs. If you want the function to be input the vector of alphas you should pass it [x(:,1); x(:,2)]. Also your function does not use its input, it uses a variable which isn't available to it. Remember the function can only use inputs you pass it. In your case, this would be qe. So inside the function it should be using qe[1] or qe[2] instaed of x(1), x(2). On another note, qe is the output, not the input. The proper name for the function would be function qe = fkin2R(alpha)
I think I follow. I am working on it now. I will be back with results. Thanks
Just to be a little more clear the function should look like this: function qe = fkin2R(alpha) qe(1) = function of alpha qe(2) = function of alpha qe = [qe(1); qe(2)]; end
this line may not even be necessary: qe = [qe(1); qe(2)];
working on it
Yeah you're fine. I'm not waiting on you or anything
I get to graphs of horizontal dots. I do not think that is correct. doPart = 'b'; switch doPart case 'a' tspan = [0 , 40]; x0 = [pi/4 0]; [t, x] = ode45( @hw2_3a , tspan , x0 ); case 'b' alpha = [x(:,1) x(:,2)]; [qe] = fkin2R(alpha); figure(1); plot(t, qe(1)); figure(2); plot(t, qe(2)); end function qe = fkin2R(alpha) qe(1) = cos(alpha(1)) + .5.*cos(alpha(1) + alpha(2)); qe(2) = sin(alpha(1)) + .5.*sin(alpha(1) + alpha(2)); end
can you paste your code from @hw2_3a?
Oh I htink i see why...you are only doing part "b" You really need to do everything. Remove the switch...case part of the code.
think*
I still get horizontal ldots.
post you hw23a code
doPart = 'b'; % Set to 'a' or 'b' as desired. switch doPart case 'a' tspan = [0 , 40]; x0 = [pi/4 0]; % Should be a column vector. [t, x] = ode45( @hw2_3a , tspan , x0 ); figure(1); plot(t, x(:,1)* 180/pi); title('\alpha1 vs Time'); xlabel('time (s)'); ylabel('\theta (degrees)'); grid on; figure(2); plot(t, x(:,2) * 180/pi); % Factor is to convert to degrees from radians. title('\alpha2 vs Time'); xlabel('time (s)'); ylabel('\theta (degrees)'); case 'b' tspan = [0 , 40]; x0 = [pi/4 0]; % Should be a column vector. [t, x] = ode45( @hw2_3a , tspan , x0 ); alpha = [x(:,1) x(:,2)]; [qe] = fkin2R(alpha); figure(1); plot(t, qe(1)); title('Xe vs Time'); xlabel('time (s)'); ylabel('Xe'); grid on; figure(2); plot(t, qe(2)); title('Ye vs Time'); xlabel('time (s)'); ylabel('Ye'); end part a function xdot = f(t, x) xdot = zeros(2,1); % Force to be a 2x1 (column) vector. xdot(1) = .5.*cos(t); % Add in differential equations. xdot(2) = 1/3; end part b function qe = fkin2R(alpha) for i = 1 : 81 qe(:,1) = cos(alpha(:,1)) + .5.*cos(alpha(:,1) + alpha(:,2)); qe(:,2) = sin(alpha(:,1)) + .5.*sin(alpha(:,1) + alpha(:,2)); end end
None of your code actually happens because the doPart variable is never set. You need to remove the case 'a' and case 'b' and switch doPart lines
Or put back the line where you set the doPart variables if you like
Oh I'm sorry I see now you're doing it fine
does not the doPart = 'b' at the top of the code make the program do case 'b' ?
Yes. Never mind on that. Does the part a work?
yes. I get two plots and no errors.
One is a sinusoid and the other an upward sloping line yes?
I did get that
Ok good. In your fkin2R function you dont need to have a loop. The way you're figuring out qe is a vector operation and all the operations already occur.
I changed the fkin2R code function qe = fkin2R(alpha) qe(1) = cos(alpha(1)) + .5.*cos(alpha(1) + alpha(2)); qe(2) = sin(alpha(1)) + .5.*sin(alpha(1) + alpha(2)); end
I do still get horizontal dots.
alpha = [x(:,1) x(:,2)]; => define this with a semicolon: alpha = [x(:,1); x(:,2)];
Ok done
Did that help?
I still get horizontal dots for my plots.
Ok see what output you're getting for qe. Just take the semicolon away from the end of the line where you fall fkin2R and see what kind of numbers you get. Are they all the same or different?
call*
qe returns qe = 0.7071 1.2071
so it's only returning 2 values? It should be a whole matrix right
yes
Take away that semicolon I told you to put in and try it again
I removed the semicolon and qe returns qe = 0.7071 1.2071
Hmm ok no difference. Ok take away the semicolon at the end of this line: alpha = [x(:,1) x(:,2)]; and we can see what the alphas look like
I did. I get two colomns of numbers for alpha.
Ok that's good. somewhere we are losing the rest of the data...
whats the output of this line? cos(alpha(:,1))
a column of numbers between 0.2 to 0.9
function qe = fkin2R(alpha) qe(:,1) = cos(alpha(:,1)) + .5.*cos(alpha(:,1) + alpha(:,2)); qe(:,2) = sin(alpha(:,1)) + .5.*sin(alpha(:,1) + alpha(:,2)); end That should be your fkin2R function right
yes
The most recent thing you posted, you removed the colons
I just put them back and ran the program and got horizontal dots
I'm out of battery. I'll look at it some more during class
thanks
Still no luck?
Oh I got it. When you plot, you not specifying (;,1) (;,2). It should look like this: plot(t, qe(:,1));
You see you're getting the right values but only plotting the first x and first y value
I got two graphs that are sinusoidal.
Well they're not purely sinusoidal right... I mean they have peaks and valleys but the amplitude is not the same or anything. It definitely has a sinusoidal aspect to it.
They are diffierent from eachother but they both follow the pattern of repeating three different peak amplitudes.
Well what do you think? Does it make sense?
I have been at this one problem for so long I no longer understand what it should look like.
I now need to figure out part c, can you help?
part c is just plotting x vs y. A parametric plot just plots both x and y as a position of time. Since you've defined the x and y according to the same time vector, it's just plotting x vs. y. This will basically show you the path the end effector would take.
I think I have that. Can you explain how I can understand the results of part b. How would I know if my plots are in the ballpark or not?
Well one thing you could do is calculate a couple points by hand and see if they match the graph. the first point is easy enough. Also if you think about what the second joint is doing...it's going in a constant speed but since it's revolute it goes around in a circle. So you would expect there to be some repetition in the end effector position.
Look at the first point for instance. alpha1 is 45 and alpha2 is 0 so the whole manipulator would be a straight line at 45 degrees right. This would imply that the x and y should be the same for the first point
My part c looks like an infinity symbol. I think that is correct. Thank you for your help.
No problem happy to help
Join our real-time social learning platform and learn together with your friends!