MECH 322: Lab Assignment 16

Octave Conditionals Practice (80 points)

Purpose:

  • Create more complex programs that include conditional branching
  • Use the if-end statement
  • Use the if-else-end statement
  • Use the if-elseif-else-end statement

Tasks to Complete On Your Own:

75/80

Problem 9 Number (10/10)

Problem 12 Quadroots (15/15)

Problem 26 BMI (14/15) Did not round BMI to one decimal place

Problem 35 Grades (20/20)

Problem 34 Cam (16/20) Did not save a png of the plot. Did not successfully run, calculation error; missing math operator between parentheses

  1. Click on + New then the File pulldown to create a new Octave (.m) script files for each of the following problems from Chapter 6. Include comments in each with your name and the problem number. Use the names given below with your FerrisID. Be sure to follow any additional instructions given as well as those in the textbook.

    • Problem 9, number9_FerrisID.m (10 points): (use a while-end loop and if-else commands)
    • Problem 12, quadroots_FerrisID.m (15 points)
    • Problem 26, bmi_FerrisID.m (15 points)
    • Problem 35, grades_FerrisID.m (20 points)
    • Problem 34, cam_FerrisID.m (20 points)
  2. Include a disp command near the beginning of each script that states the problem number. Also, clear all variables and clear the screen at the beginning of each script.

  3. Since some programs require the use of the input command and the Jupyter notebook interface does not support this command you will need to start an Octave session by clicking on "Octave Terminal.term" in the assignment folder in order to run your programs.

  4. After testing and degbugging all of the programs start logging/recording your session by typing diary lab16_practice_FerrisID.txt using your FerrisID, i.e. diary lab16_practice_bradyb.txt

  5. Create the comment % MECH 322: Lab 16 - Conditional Practice

  6. Create a comment line with your name

  7. Run each of the scripts by typing the script name without the .m extension. Insert a few blank lines between each script execution by hitting the return key

  8. Issue the command diary off to stop recording your session

  9. Your diary file lab16_practice_FerrisID.txt and the six script files you created will be automatically collected for grading after the due date.

Changes to Problem 34 Formulas

The author apparently misread his Kinematics textbook or had a bad one. Please use the following formulas instead.

$$\begin{align} y&=\frac{6}{\pi}\left[2\theta-0.5\sin{4\theta}\right] &0\leq\theta\leq\pi/2\\ \\y&=6 &\pi/2 \leq\theta\leq2\pi/3\\ \\y&=3+1.5\left[1+\cos{\left(1.5\theta-\pi\right)}\right] &2\pi/3\leq\theta\leq4\pi/3\\ \\y&=3 &4\pi/3 \leq\theta\leq3\pi/2\\ \\y&=3-6\left(\frac{2\theta-3\pi}{\pi}\right)^2 &3\pi/2\leq\theta\leq 7\pi/4\\ \\y&=6\left(1-\frac{2\theta-3\pi}{\pi}\right)^2 &7\pi/4\leq\theta\leq 2\pi \end{align}$$
In [4]:
% Problem 35 - Grades
% Chase Benson

Q=input('Enter 6 quiz scores in a vector surrounded by [ ]:');
M=input('Enter 3 midterm grades in a vector:');
F=input('Enter final exam score:');

Qavg=sort(Q)
Q(1)=[];
Qavg=mean(Q)/10;
Qweight=Qavg*.3;

Mavg=mean(M)/100;
F=F/100;

if Mavg>F
    Mweight=Mavg*.5;
    Fweight=F*.2;
else
    Mweight=Mavg*.2;
    Fweight=F*.5;
end

grade=(Qweight+Mweight+Fweight)*100;

if grade < 60
    letter="an E";
elseif grade < 70
    letter="a D";
elseif grade < 80
    letter="a C";
elseif grade < 90
    letter="a B";
else
    letter="an A";
end

printf('The students overall score is %.0f and they received an %s\n',grade,letter)
Enter 6 quiz scores in a vector surrounded by [ ]:[6 10 6 8 7 8]
Enter 3 midterm grades in a vector:[82 95 89]
Enter final exam score:81
Qavg =

    6    6    7    8    8   10

grade =  83.933
The students overall score is 84 and they received an a B
In [1]:
% Problem 9 - number 9
% Chase Benson

clear

i=0;
s=0;
while s<=120
    i=i+1;
    if rem(i,2)==0 && rem(i,13)==0 && rem(i,16)==0
        s=sqrt(i);
    end
end
fprintf('The required number is: %i\n',i)
The required number is: 14560
In [2]:
% Problem 12 - Quad roots
% Chase Benson

clear

for k=1:3
    disp('For the equation ax^2+bx+c')
    a=input('Enter a:  ');
    b=input('Enter b:  ');
    c=input('Enter c:  ');
    D=b^2-4*a*c;
    if D<0
        fprintf('\nThe equation has no real roots.\n\n')
    elseif D==0
        root=-b/(2*a);
        fprintf('\nThe equation has one root.\n\n')
        fprintf('  %.3f\n\n',root)
    else
        r1=(-b+sqrt(D))/(2*a);
        r2=(-b-sqrt(D))/(2*a);
        fprintf('\nThe equation has two roots.\n\n')
        fprintf('  %.3f  and  %.3f\n\n',r1,r2)
    end
end
For the equation ax^2+bx+c
Enter a:3
Enter b:6
Enter c:3

The equation has one root.

  -1.000

For the equation ax^2+bx+c
Enter a:-3
Enter b:4
Enter c:-6

The equation has no real roots.

For the equation ax^2+bx+c
Enter a:-3
Enter b:7
Enter c:

The equation has two roots.

  
  
In [4]:
% Problem 26 - BMI
% Chase Benson

clear

for j=1:2
    W=input('Input weight in lb: ');
    h=input('Input height in in: ');
    BMI=703*W/h^2;
    if BMI<18.5
        fprintf('BMI value %.1f, which means underweight\n\n',BMI)
    elseif BMI<25
        fprintf('BMI value %.1f, which means normal\n\n',BMI)
    elseif BMI<30
        fprintf('BMI value %.1f, which means overweight\n\n',BMI)
    else
        fprintf('BMI value %.1f, which means obese\n\n',BMI)
    end
end
Input weight in lb:180
Input height in in:74
BMI value 23.1, which means normal

Input weight in lb:150
Input height in in:61
BMI value 28.3, which means overweight

In [4]:
% Problem 34 - Cam
% Chase Benson

clear

theta=linspace(0,2*pi,100);
for i=1:100
    if theta(i)<=pi/2
        y(i)=6*(2*theta(i)-0.5*sin(theta(i)))/pi;
    elseif theta(i)<=2*pi/3
        y(i)=6;
    elseif theta(i)<=4*pi/3
        y(i)=6-3*(1-0.5*cos(3*(theta(i)-2*pi/3)));
    elseif theta(i)<=3*pi/2
        y(i)=3;
    elseif theta(i)<=7*pi/4
        y(i)=3-1.5*((theta(i)-3*pi/2)(pi/4))^2;
    else
        y(i)=0.75-0.75*(1-(theta(i)-7*pi/4)/(pi/4))^2;
    end
end

plot(theta,y)
xlabel('rotation angle, rad')
ylabel('follower displacement, cm')
error: subscript indices must be either positive integers less than 2^31 or logicals
error: __plt2vv__: vector lengths must match
error: called from
    __plt__>__plt2vv__ at line 487 column 5
    __plt__>__plt2__ at line 246 column 14
    __plt__ at line 113 column 17
    plot at line 220 column 10
In [ ]:

In [ ]: