In: Computer Science
The following table gives the approximate values of the coefficient of static friction μ, for various materials
Option/ Materials/ μ
1 /Metal on metal/ 0.2
2 /Wood on wood /0.35
3/ Metal on wood /0.4
4/ Rubber on concrete/ 0.7
To start moving a weight W, on a horizontal surface, you must
push with a force F, where F=μW.
Write an m-file that achieves the following:
1. Uses fprintf to print out the option and material information to
the command window
2. Prompt the user to input a value of W and the type of
materials/option
Check if the input values are valid (e.g. negative weights or
non-integer/incorrect option numbers). Your
program should continue prompting the user to enter values until
they are valid.
3. Use a switch statement to compute the force required. Use
fprintf to print a statement including the materials
used and the force required.
Hint: You may want to use a while loop to ensure that the user
enters valid inputs.
clc,clear
s=0;
f=0;
fprintf("The data table is\n");
fprintf('Option \t Materials \t Coeff.\n');
fprintf(' 1 \t Metal_on_metal \t 0.2\n');
fprintf(' 2 \t Wood_on_wood \t 0.35\n');
fprintf(' 3 \t Metal_on_wood \t 0.4\n');
fprintf(' 4 \t Rubber_on_concrete \t 0.7\n');
prompt = 'Enter the weight of material ';
z = 0;
while z<=0 || ~(isinteger(z))
z= input(prompt);
end
s=input('Enter your option');
while s ~= '1,2,3,4';
s = input('Chooose your option from (1,2,3,4) ', 's');
end
switch s
case 1
f=0.2*z;
fprintf('%f',f);
case 2
f=0.35*z;
fprintf('%f',f);
case 3
f=0.4*z;
fprintf('%f',f);
case 4
f=0.7*z;
fprintf('%f',f);
otherwise
printf("Sorry");
end