Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Matlab use a menu to ask the user whether they want to predict an elongation

ID: 3920105 • Letter: I

Question

In Matlab use a menu to ask the user whether they want to predict an elongation in inches for a specified spring or spring combination given a mass OR o to predict a required mass in grams for a specified spring or spring combination given a desired elongation. If the user enters an elongation outside of 0 to 30 inches, inform them of the range restrictions and ask them again to enter an elongation until they enter a valid elongation. Similarly, if the user enters a mass outside of 0 to 2000 grams, inform them of the range restrictions and ask them again to enter a mass until they enter a valid mass. Repeat this until the user enters valid input values.

Use a menu to ask the user to indicate the configuration of springs they wish to use: individual springs, 2 springs in parallel, 2 springs in series, Sp1+Sp2 in parallel with a 3rd spring in series with that pair and Sp1+Sp2 in series with a 3rd spring in parallel with that pair. Ask the user to indicate the color of spring or springs used. The code should ask the user for only the number of springs required for the selected configuration. So, if the user selects the individual spring configuration, the code should ask for only one spring color

I need to use while loops to complete this, but I cannot get it to work. Please help. Here is my work below

clear

clc

Options={'Predict an elongation in inches [in] for a specified spring or spring combination given a mass [g]';'Predict a required mass in grams [g] for a specified spring or spring combination given a desired elongation [in]'};

UserChoice=menu('Choose an option',Options(1,1),Options(2,1));

if UserChoice==1

InputData=input('Enter a mass value between the values of 0 to 2000 grams [g]');

elseif UserChoice==2

InputData=input('Enter an elongation value between the values of 0 to 30 inches [in]');

elseif UserChoice==0

error('You did not choose an option within the menu');

end   

if UserChoice==1

while InputData>0 || InputData<2000

warning('You did not enter a value between 0 to 2000');

InputData=input('Enter a mass value between the values of 0 to 2000 grams [g]');

end

end

Explanation / Answer

Hi,

Please find the answer below:

You have specified conflicting conditions in the while loop.

while InputData>0 || InputData<2000

Take for example take valid mass value 250 grams

This value is a valid as per the specification. But this satisfies your first condition

i.e InputData>0 becoz 250 > 0

and prompts user to enter another input.

Fix:

while InputData < 0 || InputData > 2000
        warning('You did not enter a value between 0 to 2000');
        InputData=input('Enter a mass value between the values of 0 to 2000 grams [g]');
    end


Same way for the other condition

while InputData < 0 || InputData > 30
        warning('You did not enter a value between 0 to 30');
        InputData=input('Enter an elongation value between the values of 0 to 30 inches [in] ');
    end
  
sample code:

clear
clc
Options={'Predict an elongation in inches [in] for a specified spring or spring combination given a mass [g]';'Predict a required mass in grams [g] for a specified spring or spring combination given a desired elongation [in]'};
UserChoice=menu('Choose an option',Options(1,1),Options(2,1));
if UserChoice==1
    InputData=input('Enter a mass value between the values of 0 to 2000 grams [g]');
    while InputData < 0 || InputData> 2000
        warning('You did not enter a value between 0 to 2000');
        InputData=input('Enter a mass value between the values of 0 to 2000 grams [g]');
    end
elseif UserChoice==2
    InputData=input('Enter an elongation value between the values of 0 to 30 inches [in]');
    while InputData < 0 || InputData > 30
        warning('You did not enter a value between 0 to 30');
        InputData=input('Enter an elongation value between the values of 0 to 30 inches [in] ');
    end
elseif UserChoice==0
    error('You did not choose an option within the menu');
end


Sample test:

Enter a mass value between the values of 0 to 2000 grams [g] 2001
Warning: You did not enter a value between 0 to 2000
> In SpringMenu (line 8)
Enter a mass value between the values of 0 to 2000 grams [g]300


Hope this is helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote