my code doesnt work
Show older comments
% Define the given parameters
AB = 6.4; % cm
BC = 27.0; % cm
CD = 19.2; % cm
EF = 31.1; % cm
EG = 29.1; % cm
GH = 16.0; % cm
AD = [25 -11]; % cm
AH = [40 24]; % cm
r = 7; % cm
theta_dot = 20; % RPM
% Calculate the angle of the crank AB
theta = 0:0.01:2*pi;
cos_alpha = (AB^2 + BC^2 - CD^2 - 2*AB*BC*cos(theta))/...
(2*sqrt(AB^2 + BC^2 - 2*AB*BC*cos(theta)));
alpha = acosd(cos_alpha);
% Calculate the position of the piston F for each angle theta
s = sqrt(EF^2 + EG^2 - 2*EF*EG*cosd(alpha) + GH^2 - 2*GH*EG*sind(alpha));
plot(theta, s);
xlabel('Crank angle \theta (rad)');
ylabel('Position of piston s (cm)');
% Calculate the speed of the piston F for each angle theta
syms alpha;
syms s;
s = sqrt(EF^2 + EG^2 - 2*EF*EG*cosd(alpha) + GH^2 - 2*GH*EG*sind(alpha));
s_dot = diff(s,alpha);
theta_mid = (theta(1:end-1) + theta(2:end)) / 2;
plot(theta_mid, s_dot);
xlabel('Crank angle \theta (rad)');
ylabel('Speed of piston s'' (cm/s)');
%Calculating the torque of the engine:
% Define the given parameters
Fp_comp = [-12 0 0]; % N
Fp_exp = [0.2 0 0]; % N
% Calculate the torque of the engine for each angle theta
F_comp = Fp_comp * [-sind(alpha); cosd(alpha); zeros(size(alpha))];
F_exp = Fp_exp * [-sind(alpha); cosd(alpha); zeros(size(alpha))];
T = r * (F_comp .* s_dot)' + r * (F_exp .* s_dot)';
plot(theta_mid, T);
xlabel('Crank angle \theta (rad)');
ylabel('Torque T (Nm)');
% Define the given parameters
rho = 1000; % kg/m^3
Q = pi * r^2 * s_dot / 100; % L/s
% Calculate the mean power of the motor
P_mean = mean(T .* theta_dot * 2 * pi / 60);
disp(['The mean power of the motor is ' num2str(P_mean) ' W.']);
% Calculate the flow rate of the pump
V_dot = Q / 1000; % m^3/s
m_dot = rho * V_dot;
disp(['The flow rate of the pump is ' num2str(m_dot) ' kg/s.']);
here is the system and SN in my case is 70

Answers (1)
Walter Roberson
on 9 Mar 2023
syms alpha;
syms s;
s = sqrt(EF^2 + EG^2 - 2*EF*EG*cosd(alpha) + GH^2 - 2*GH*EG*sind(alpha));
s_dot = diff(s,alpha);
alpha is symbolic, s is symbolic in alpha, diff(s,alpha) will be symbolic.
plot(theta_mid, s_dot);
If I read correctly, all of the elements that go into making up this s are scalar. So you appear to be asking to plot() a numeric vector for the dependent variable, and a symbolic expression for the dependent variable. But the symbolic expression is not convertable to numeric form -- it involves the unresolved variable alpha . You can only use plot() with symbolic expressions in the case that the symbolic expressions can be converted to numeric form by using double()
The function that can plot symbolic expressions is fplot . But notice that your independent variable is theta, not alpha, so it is not obvious that it makes sense to substitute theta values for alpha in the expression.
1 Comment
AB = 6.4; % cm
BC = 27.0; % cm
CD = 19.2; % cm
EF = 31.1; % cm
EG = 29.1; % cm
GH = 16.0; % cm
AD = [25 -11]; % cm
AH = [40 24]; % cm
r = 7; % cm
theta_dot = 20; % RPM
% Calculate the angle of the crank AB
theta = 0:0.01:2*pi;
cos_alpha = (AB^2 + BC^2 - CD^2 - 2*AB*BC*cos(theta)) ./ ...
(2*sqrt(AB^2 + BC^2 - 2*AB*BC*cos(theta)));
You had / which is the matrix division operation, not the element-by-element division operation.
[min(cos_alpha), max(cos_alpha)]
With values outside the range -1 to +1, the arc cos is going to be imaginary.
alpha = acosd(cos_alpha);
% Calculate the position of the piston F for each angle theta
s = sqrt(EF^2 + EG^2 - 2*EF*EG*cosd(alpha) + GH^2 - 2*GH*EG*sind(alpha));
plot(theta, [real(s(:)), imag(s(:))]);
legend({'real', 'imaginary'})
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
