Question

I have been trying this problem for over 8 hours. How do I make the Matlab code for the given problem. It has to move.

- Use the . You may use cif to clear the figure for the next instant of time The goal is the same as before except this time

0 0
Add a comment Improve this question Transcribed image text
Answer #1

%%
clear
clc
time = 0:0.01:20;


%% Equations of motion
options = optimoptions('fsolve','Display','off');
x0 = [0 0 0 0]';
for i = 1:length(time)
t = time(i);
[x,fval] = fsolve(@(x) fun(x,t), x0, options);
x1(i) = x(1);
y1(i) = x(2);
x2(i) = x(3);
y2(i) = x(4);
  
end


%% Animation

link1_x = [0 x1(1)];
link1_y = [0 y1(1)];

link2_x = [x1(1) x2(1)];
link2_y = [y1(1) y2(1)];

link3_x = [x2(1) 5];
link3_y = [y2(1) 0];

link1 = plot(link1_x,link1_y, 'b' ,'linewidth',1.25);
hold on
link2 = plot(link2_x,link2_y, 'b' ,'linewidth',1.25);
hold on
link3 = plot(link3_x,link3_y, 'b' ,'linewidth',1.25);
hold off

axis([-4 8 -5 5]);
grid on

for i = 2:length(time)
link1_x(2) = x1(i);
link1_y(2) = y1(i);
link2_x = [x1(i) x2(i)];
link2_y = [y1(i) y2(i)];
link3_x(1) = x2(i);
link3_y(1) = y2(i);
  
link1.XData = link1_x;
link1.YData = link1_y;
link2.XData = link2_x;
link2.YData = link2_y;
link3.XData = link3_x;
link3.YData = link3_y;
  
title(sprintf('t = %f sec',time(i)))
drawnow limitrate
pause(0.005)
end

Function 'fun' to pass to fsolve (save as new script)

function F = fun(x, t)
%% Parameters
w = 0.5;
d = 5;
l1 = 3;
l2 = 5;
l3 = 3.5;
%% Equations
x1 = x(1);
y1 = x(2);
x2 = x(3);
y2 = x(4);
  
F = [x1 - l1*cos(w*t);
y1 - l1*sin(w*t);
(x2 - x1)^2 + (y2 - y1)^2 - l2^2;
(x2 - d)^2 + (y2)^2 - l3^2];
end

Add a comment
Know the answer?
Add Answer to:
I have been trying this problem for over 8 hours. How do I make the Matlab code for the given problem. It has to move. - Use the . You may use cif to clear the figure for the next instant of time...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT