Question

Hello. I need help modifying this MatLab code. This is a basic slider-crank animation. the current...

Hello. I need help modifying this MatLab code. This is a basic slider-crank animation. the current program stops the animation and then has the crank move backward. I need the crank to instead to move in full 360 degrees circular motion. Exactly like how a slide-crank mechanism works in real life. thank you and here is the code.

%%Code Begins Here%%

L1=0.2; L2=0.45;
W=0.5;
tt=linspace(0,15);
for i=1:length(tt)
    x2=@(t) L2+L1*sin(W*t);
    y2=0;

    X2=x2(tt(i));
    Y2=y2;

    sol= fsolve(@(x,x2,y2) root2d(x,X2,Y2), [0,0]);
    clf
    hold on
    plot(0,0,'b*','linewidth',20)
    plot(sol(1),sol(2),'ro','linewidth',15)
    plot([0 sol(1)], [0 sol(2)],'linewidth',5,'color','k')
    plot(X2,Y2,'md','linewidth',15)
    plot([X2 sol(1)], [Y2 sol(2)],'linewidth',5,'color','k')


    ylim([-0.5 0.5])
    xlim([-0.3 0.7])
    hold off
    drawnow
    pause(0.025)
  
end

function F = root2d(x,x2,y2)

    F(1) = (x(1)-x2)^2+(x(2)-y2)^2-0.45^2;
    F(2) = (x(1))^2+(x(2))^2-0.2^2;
end

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

Hi, the code is correct, however you are a victim of a classic problem of the fsolve function which arises when it tries to solve conic equations with it. The problem of the fsolve function is that it always tries to approximate a solution closest to the point of initialization. So a graphical description of the problem is as follows

Point of initialization (0,0) (x,y) Both these points are solutions for x^2+y^2 = r^2, so (x,-Y) fsolve can choose between ei

This was exactly what was happening. Fsolve simply chose to output (x,y) instead of (x,-y) and so your crank kept dancing in the upper half circle.

So the only way out of this problem is to keep a dynamic point of initialization. A graphical depication of the solution would be as follows

inital point set as (0,r) for 0 to 180 (x,y) this point is closer to (0,r), so will always converge on this 0/360 degree 180

Thus we will have to include an algorithm which will automatically detect when the crank pin is crossing 180 or 360 degrees and change the initial point accordingly. I have provided both the text and the screenshot for your better understanding. The screenshot also has some comments for your help. Please note that the screenshot shows the part where the changes are implemented. The rest of the code is the same.

3 $$Code Begins Here Ll=0.2; L2=0.45; W=0.5; tt=linspace (0,15); 4 - 5 6 This is the intial approximation of fsolve. This fli

%%Code Begins Here%%
L1=0.2; L2=0.45; W=0.5; tt=linspace(0,15);

%This is the intial approximation of fsolve. This flips between +ve and -ve value for each half cycle
a = 0.2;
%This is a flag to let the program know when the value of a is to be
%flipped and which half-cycle is currently under process
flag = 0;
for i=1:length(tt)
x2=@(t) L2 + L1*sin(W*t);
y2=0;
X2=x2(tt(i));
Y2=y2;
sol = fsolve(@(x,x2,y2) root2d(x,X2,Y2), [0,a]); %Initial conditions are to be flipped between (0,2) and (0,-2) for correct solution
if sol(1) >= 0.199 && flag == 0 %alogrithm to flip the value. Uses flag to make sure the value is flipped only once in a half cycle.
a = a * -1;
flag = 1;
elseif sol(1) <= -0.199 && flag == 1
a = a * -1;
flag = 0;
end
clf
hold on
  
%Rest of the code same....

plot(0,0,'b*','linewidth',20);
plot(sol(1),sol(2),'ro','linewidth',15);
plot([0 sol(1)], [0 sol(2)],'linewidth',5,'color','k');
plot(X2,Y2,'md','linewidth',15);
plot([X2 sol(1)], [Y2 sol(2)],'linewidth',5,'color','k');
ylim([-0.5 0.5])
xlim([-0.3 0.7])
hold off
drawnow
pause(0.025)
end

function F = root2d(x,x2,y2)

F(1) = (x(1)-x2)^2+(x(2)-y2)^2-0.45^2;
F(2) = (x(1))^2+(x(2))^2-0.2^2;
end

Naturally it is not possible for me to attach an animation of the output here, but as it worked for me I am sure the code will work for you too.

*A humble request* - If you have any doubt, please use the comment section to communicate. Please be a little patient, it is an honest promise I will reply as soon as possible. This will clarify your doubt, and also help me to get better at answering your next questions. At the same time, If my answer helped you, please consider leaving an upvote. I hope you understand my viewpoint. Thank you :)

Add a comment
Know the answer?
Add Answer to:
Hello. I need help modifying this MatLab code. This is a basic slider-crank animation. the current...
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
  • MATLAB code for a double pendulum. Please explain each lines for these codes pls. -------------------------------------...

    MATLAB code for a double pendulum. Please explain each lines for these codes pls. ---------------------------------------------------------------------------- clc close all clear all %---------Parameters------------------------------------------------------ L1=1; L2=1 ; M_1=2 ; M_2=1; G=9.8; %---------initial condition----------------------------------------------- tspan=30; theta1=3; theta1_prime=0; theta2=2.5; theta2_prime=0; y0=[theta1 theta1_prime theta2 theta2_prime]; [t,y]=ode45(@pend, [0 ,tspan],[ 3 0 2 0]); %---position of mass 1 and mass 2---------------------------------------- x1=L1*sin(y(:,1)); y1=-L1*cos(y(:,1)); x2=L1*sin(y(:,1))+l2*sin(y(:,3)); y2=-L1*cos(y(:,1))-l2*cos(y(:,3)); %------visualizing the result--------------------------------------------- figure(1) plot(x1,y1,'linewidth',2) hold on plot(x2,y2,'r','linewidth',2) h=gca; get(h,'fontSize') set(h,'fontSize',14) xlabel('X','fontSize',14); ylabel('Y','fontSize',14); title('Chaotic Double Pendulum','fontsize',14) fh = figure(1); set(fh, 'color', 'white'); figure(2)...

  • 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...

    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 you will plot a moving 3 bar lin mechanism shown below kage. Use the L2 (k2,ya L3 The equations that govern the motion of this linkage are:...

  • Hello, i have this matlab code where i have to track a red ball from and...

    Hello, i have this matlab code where i have to track a red ball from and uploaded video, and plot its direction in x-y direction My Question is how can i add x vs time plot and y vs time plot in this code. Thank you if exist('V','var') delete(V); end clear;clc; V=VideoReader('video.mp4'); figure(1); x=[];y=[]; i=1; while hasFrame(V) J=readFrame(V); figure(1);imshow(J);title(['Frame No. ',num2str(i)]); if (i>=28 && i<=132) bw=J(:,:,1)>200 & J(:,:,2)<80 & J(:,:,3)<80; bw=imfill(bw,'holes'); A=regionprops(bw,'Centroid'); x(end+1)=A.Centroid(1); y(end+1)=A.Centroid(2); hold on; plot(x,y,'*k');hold off; end i=i+1;...

  • please help me with this MATLAB CODE and explain to me what each line does and...

    please help me with this MATLAB CODE and explain to me what each line does and what is used for? leave your comments as words, not as pictures. ..................................................................................................................................................................... clear all; close all; % For a script file, you better start with clear all and close all                        % However, for a fucntion, you better NOT to start                        % with them %% End of cell mode example %% Plot function t = 0:0.1:5; x1 = sin(2*5*t); x2 = cos(3*7*t);...

  • Is the function being called correctly? format shorteng r = linspace(0.1,4,500); % Distance in Angstrom re...

    Is the function being called correctly? format shorteng r = linspace(0.1,4,500); % Distance in Angstrom re = 0.7414; % Equilibrium Seperation D = 38292; % Dissoc Enrgey [cm^-1] twoBeta = 1.4426; Vm = morse_potential(D, twoBeta, re, r); % Call Morse Potential Function plot(r,Vm, 'g-','Linewidth',2) xlabel('Distance [A]') ylabel('Potential Energy [cm^-1]') title('Morse Potential of Hydrogens') ylim([0,5e4]) grid 'on' hold on D2 = 38292; % Equiibrium distance re2 = 0.7413; % Width of the potential well for H2 twoBeta2 = 1.4433; r =...

  • I need help in MATLAB. I'm working on a circuits lab report and I want to...

    I need help in MATLAB. I'm working on a circuits lab report and I want to plot the derivative of an input signal. The circuit is a differentiator OpAmp. It is receiving a triangle wave as an input and should output a square wave. (I've included my existing code.) The output formula is: Vout = -(Rf)*C*(dVin/dt) Where Rf is feedback resistance: Rf = 1*10^6; and C = 1*10^-6. EXISTING CODE: %% This section is copied, and then modified from another...

  • PLEASE HELP WITH THE FOLLOWING R CODE! I NEED HELP WITH PART C AND D, provided...

    PLEASE HELP WITH THE FOLLOWING R CODE! I NEED HELP WITH PART C AND D, provided is part a and b!!!! a) chiNum <- c() for (i in 1:1000) { g1 <- rnorm(20,10,4) g2 <- rnorm(20,10,4) g3 <- rnorm(20,10,4) g4 <- rnorm(20,10,4) g5 <- rnorm(20,10,4) g6 <- rnorm(20,10,4) mse <- (var(g1)+var(g2)+var(g3)+var(g4)+var(g5)+var(g6))/6 M <- (mean(g1)+mean(g2)+mean(g3)+mean(g4)+mean(g5)+mean(g6))/6 msb <- ((((mean(g1)-M)^2)+((mean(g2)-M)^2)+((mean(g3)-M)^2)+((mean(g4)-M)^2)+((mean(g5)-M)^2)+((mean(g6)-M)^2))/5)*20 chiNum[i] <- msb/mse } # plot a histogram of F statistics h <- hist(chiNum,plot=FALSE) ylim <- (range(0, 0.8)) x <- seq(0,6,0.01) hist(chiNum,freq=FALSE, ylim=ylim)...

  • Please help me with this MATLAB programming problem! Im coding iin MATLAB2018 if that makes any d...

    Please help me with this MATLAB programming problem! Im coding iin MATLAB2018 if that makes any difference! The first picture is the question i need to answer. The picture below is used as reference to answer the question. The last picture (below) is the into to the problem, and is used as reference. 1. Use Matlab to create the following single plot with three subplots. All titles, gridlines, and axis labels should be as shown. Arc System Response 15 E...

  • I DESPERATELY NEED HELP WITH THIS DIFFERENTIAL EQUATIONS MATLAB ASSIGNMENT IM SUPPOSED TO BE LEARNING BUT...

    I DESPERATELY NEED HELP WITH THIS DIFFERENTIAL EQUATIONS MATLAB ASSIGNMENT IM SUPPOSED TO BE LEARNING BUT WE HAVE A SUB AND HE DIDN'T TEACH IT! ITS EULER AND IMPROVED EULER IN MATLAB! HERE IS THE LINK FOR THE IMAGE FILE THAT SHOWS THE FULL INSTRUCTIONS FOR THE CODE. https://imgur.com/a/gjmypLs Also, here is my code so far that I borrowed form an old assignment but the data is all wrong and the application of the code is slightly different so either...

  • All I need help with is the matlab codes for B) and C) Photos of answers are below. Rate will be ...

    All I need help with is the matlab codes for B) and C) Photos of answers are below. Rate will be given for correct matlab code! DP6.3 A unity negative feedback system with K(s + 2) has two parameters to be selected. (a) Determine and plot the regions of stability for this system. (b) Select r and K so that the steady-state error to a ramp input is less than or equal to 25% of the input magnitude. (c) Determine...

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