Matlab Please do not hardcode into the menu Use cell array as stated in the ques
ID: 2086213 • Letter: M
Question
Matlab Please do not hardcode into the menu Use cell array as stated in the question to get the gravitonal constant without using conditional statement Thank you ! Challenge Question 4 The relationship between the frequency of oscillation (f, in hertz), gravity (g in meters per second), and the length (L, in meters) is f 2T given by the equation shown. Enter the following data into the program. The cell array contains the planet irn Row 1, and the corresponding gravity in units of meters per second squared in Row 2 G= {'Earth ' ,'Mars ' , 'Venus ' ; 9.81,3.71,8.87) Ask the user the following questions: From a menu entitled "Choose a planetthe user can choose Earth, Mars, or Venus. Use the cell array G to determine the gravitational constant based on the planet chosen from the menu. Enter the pendulum length, in meters. Create a formatted output statement for the user in the command window similar to the following. The decimal places must match. on Earth, a 0.2 meter pendulum has a frequency of 0.90 Hz Thinking Lie an Engineer n Active Leaming ApproachExplanation / Answer
Here is the matlab code for the question.
To change given data of planets just change the 'G' matrix.
%=======================================================================================%
%Given Data for Planets and their 'g' value
G = {' Earth',' Mars',' Venus'; 9.81,3.71,8.87};
%Create a selection box for selecting the planet
fn = G(1,:); %Passing names of planets
[indx,tf] = listdlg('PromptString','Choose a planet:',...
'SelectionMode','single',...
'ListString',fn);
%indx will have the index of the planet chosen
g = G(2,indx); %Gravitational constant chosen from table
gr = cell2mat(g); %Convert to numeric value
prompt = {'Enter length of pendulum in meter:'};
title = 'Input Length';
dims = [1 35];
l = inputdlg(prompt,title,dims); %Length input
ln = str2double(l); %Convert to numeric value
f = 2.*pi.*sqrt(ln./gr);
f = round(f,2);
f = num2str(f,2); %2 decimal points
ws = ' '; %for adding white space
answer = strcat('On',G(1,indx),' a',ws,l,' meter pendulum has a frequency of',ws,f,' Hz')
%========================================================================================%