error: too many output arguments

1 view (last 30 days)
Karolina Kolodziej
Karolina Kolodziej on 19 Feb 2019
Answered: Karolina Kolodziej on 19 Feb 2019
Hi! I'm new to MATLAB, so I'd appreciate it if someone could help me. Here is my code for the position analysis of a slider-crank mechanism. Whenever I try to run the code, the command window says: "Error using UnitVector: Too many output arguments. Error in SliderCrank2 (line 29): [e1,n1] = UnitVector(0);"
% SliderCrank_Position_Analysis.m
% performs a position analysis on the slider-crank linkage and
% plots the piston position as a function of crank angle
% by Karolina Kolodziej, February 20, 2019
clear, clc;
a = 0.10; % crank length (m)
b = 0.26; % connecting rod length (m)
c = 0.0; % vertical slider offset
% Ground pins
x0 = [0;0]; % ground pin at A (origin)
% solving for conn. rod angle (theta 3) and piston pos. (d)
N = 361; % # of times to perform calc.
[xB,xC] = deal(zeros(2,N)); % allocate space for pins B and C
[theta2, theta3, d] = deal(zeros(1,N)); %".." for link angles
% Main loop
for i = 1:N
theta2(i) = (i-1)*(2*pi)/(N-1);
theta3(i) = asin((c-a*sin(theta2(i)))/b);
d(i) = a*cos(theta2(i)) + b*cos(theta3(i));
% calculate unit vectors
[e1,n1] = UnitVector(0);
[e2,n2] = UnitVector(theta2(i));
[e3,n3] = UnitVector(theta3(i));
% solve for position of point B
xB(:,i) = FindPos(x0,a,e2);
xC(:,i) = FindPos(xB(:,i),b,e3);
end
plot(theta2*180/pi,d*100,'o','Color',[153/255 153/255 153/255])
hold on
plot(theta2*180/pi,xC(1,:)*100,'Color',[0 110/255 199/255])
title('Piston Position versus Crank Angle for Slider-Crank')
xlabel('Crank angle (degrees)')
ylabel('Position (cm)')
legend('d','xC')
grid on
  2 Comments
Stephen23
Stephen23 on 19 Feb 2019
And what exactly is UnitVector ? How is it defined?
Karolina Kolodziej
Karolina Kolodziej on 19 Feb 2019
I think the foundation of it should look something like this.. I am not sure if I'm supposed to write 3 separate files for each variable e1, e2, e3, or if its all the same one.
function [e,n] = UnitVector(theta)
e = [ cos(theta); sin(theta)]; n = [-sin(theta); cos(theta)];

Sign in to comment.

Answers (1)

Karolina Kolodziej
Karolina Kolodziej on 19 Feb 2019
I figured it out. There was a missing function to find position:
Main Code:
% SliderCrank_Position_Analysis.m
% performs a position analysis on the slider-crank linkage and
% plots the piston position as a function of crank angle
% by Karolina Kolodziej, February 20, 2019
clear, clc;
a = 0.10; % crank length (m)
b = 0.26; % connecting rod length (m)
c = 0.0; % vertical slider offset
% Ground pins
x0 = [0;0]; % ground pin at A (origin)
% solving for conn. rod angle (theta 3) and piston pos. (d)
N = 361; % # of times to perform calc.
[xB,xC] = deal(zeros(2,N)); % allocate space for pins B and C
[theta2, theta3, d] = deal(zeros(1,N)); %".." for link angles
% Main loop
for i = 1:N
theta2(i) = (i-1)*(2*pi)/(N-1);
theta3(i) = asin((c-a*sin(theta2(i)))/b);
d(i) = a*cos(theta2(i)) + b*cos(theta3(i));
% calculate unit vectors
[e1,n1] = UnitVector(0);
[e2,n2] = UnitVector(theta2(i));
[e3,n3] = UnitVector(theta3(i));
% solve for position of point B
xB(:,i) = FindPos(x0,a,e2);
xC(:,i) = FindPos(xB(:,i),b,e3);
end
plot(theta2*180/pi,d*100,'Color',[153/255 153/255 153/255])
hold on
plot(theta2*180/pi,xC(1,:)*100,'linewidth',2,'Color',[0 110/255 199/255])
title('Piston Position versus Crank Angle for Slider-Crank')
xlabel('Crank angle (degrees)')
ylabel('Position (cm)')
grid on
UnitVector:
function [e,n] = UnitVector(theta)
e=[cos(theta);sin(theta)];
n=[-sin(theta);sin(theta)];
end
FindPos:
function x= FindPos(x0,L,e)
x=x0+L*e;

Categories

Find more on Statics and Dynamics in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!