SO, I have this problem, which I solved for dynamics, but now I should write a code and then generate two plots. (5.38)
I found a solution in Mathematica software, but I can't figure out what am I doing wrong in MATLAB. Here's my code:
% Given
syms theta;
W_A=100;
m_A=W_A/32.2;
W_B=20;
m_B=W_B/32.2;
L=15;
theta_0= deg2rad(70);
% Solve the system of three equations
one = str2sym('W_A*L*(cos(theta)-cos(theta_0))-1/2*m_A*V_A^2-1/2*m_B*V_Bx^2+W_A*L');
two = str2sym('m_B*V_Bx+m_A*(V_Bx+L*theta_1*cos(theta))');
three = str2sym('-V_A^2+(V_Bx+ L*theta_1*cos(theta))^2+(L*theta_1*sin(theta))^2');
[theta_1,V_A,V_Bx] = solve(one,two,three);
% Plot the velocity of the trolley and the speed of the rider as a function of the angle θ of the first half of a full swing
fplot(V_Bx, [-70,70], 'Linewidth',2)
hold on
fplot(V_A, [-70,70], '-.*c')
hold off
% Plot the velocity of the trolley and the speed of the rider as a function of the angle θ of the second half of a full swing
fplot(V_Bx, [-70,70], 'Linewidth',2)
hold on
fplot(V_A, [-70,70], '-.*c')
hold off
Thank you so much in advance!!!

답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 11월 15일
편집: Ameer Hamza 2020년 11월 17일

0 개 추천

Following shows how to write this in MATLAB.
syms theta theta_1 V_A V_Bx;
W_A=100;
m_A=W_A/32.2;
W_B=20;
m_B=W_B/32.2;
L=15;
theta_0= deg2rad(70);
% Solve the system of three equations
one = W_A*L*(cos(theta)-cos(theta_0))-1/2*m_A*V_A^2-1/2*m_B*V_Bx^2+W_A*L;
two = m_B*V_Bx+m_A*(V_Bx+L*theta_1*cos(theta));
three = -V_A^2+(V_Bx+ L*theta_1*cos(theta))^2+(L*theta_1*sin(theta))^2;
[theta_1,V_A,V_Bx] = solve(one,two,three);
% Plot the velocity of the trolley and the speed of the rider as a function of the angle θ of the first half of a full swing
fplot(V_Bx, [-70,70], 'Linewidth',2)
hold on
fplot(V_A, [-70,70], '-.*c')
hold off
% Plot the velocity of the trolley and the speed of the rider as a function of the angle θ of the second half of a full swing
fplot(V_Bx, [-70,70], 'Linewidth',2)
hold on
fplot(V_A, [-70,70], '-.*c')
hold off
The above can take a long time to plot because there are 12 solutions given by solve() functions. To speed up the computation, you can convert the functions to numeric function handle using matlabFunction() and then use fplot().

댓글 수: 7

Kseniia Kudriavtceva
Kseniia Kudriavtceva 2020년 11월 17일
But you did nothing to my code.
Just copied. Why do you do so?
I can't put numeric function, because I don't have it.
Walter Roberson
Walter Roberson 2020년 11월 17일
Your code used str2sym
Walter Roberson
Walter Roberson 2020년 11월 17일
편집: Walter Roberson 2020년 11월 17일
When you use str2sym then there is no connection made between variables you mention and variables that you set at the MATLAB level with the same name.
A=1
eqn = str2sym('A')
eqn will not be a symbolic 1, it will be a reference to a symbolic variable named A that lives inside the symbolic engine.
You can get a numeric function by using
matlabFunction(V_Bx)
Kseniia Kudriavtceva
Kseniia Kudriavtceva 2020년 11월 17일
There's unrecognized function or variable 'theta'
Kseniia Kudriavtceva
Kseniia Kudriavtceva 2020년 11월 17일
And here's the graph which I'm getting when put thets as variable in the beggining after syms
Ameer Hamza
Ameer Hamza 2020년 11월 17일
편집: Ameer Hamza 2020년 11월 17일
Yes, there was a theta missing in the first line. I have updated the answer. This graph is likely according to your equations.
Kseniia Kudriavtceva
Kseniia Kudriavtceva 2020년 11월 18일
Nope, here's what I should get as a result

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2020년 11월 15일

댓글:

2020년 11월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by