Question

In: Mechanical Engineering

Part 2: MATLAB Exercise 1 (50 pts) Write a program that tells the user if a...

Part 2: MATLAB

Exercise 1 (50 pts)

Write a program that tells the user if a city of their choice is in a tropical, temperate, or polar region. Use the cities.txt file provided in the Files section of this class. Your program should read the data in that file, find the city given by the user, and return a text message with the information. If the city is not found, your program should write a message accordingly. To decide which region each city falls into, use the table below.

Latitude Region
Above 66 N Polar
Between 35 N and 66 N Temperate
Between 35 S and 35 N Tropical
Between 35 S and 66 S Temperate
Below 66 S Polar

HINT: If you follow the file reading methods explained in our video tutorial, your data will be in cell format. There are a few functions that could be very useful in this case: strcmp compares two strings (one of which can be in a cell) to see if they match. Also, cell2mat converts a cell array into an ordinary array. Finally, find finds indices and values of any non-zero elements in an array.

See the output examples below:

What city are you interested in? Caracas
Caracas is in a tropical region 

What city are you interested in? Merced
City not found! 

https://catcourses.ucmerced.edu/files/1587454/download?download_frd=1

Solutions

Expert Solution

clc

% declaring filename
filename = 'cities.txt';
% reading file
[data] = readFile(filename);
% declaring latitude array
latitude = [];
% getting cities
cities = lower(data{3});
%reading the latitudes and storing in the variables
for ii = 1:numel(data{1})
tokens = strsplit(char(data{1}(ii)), '°');
latitude = [latitude, str2num(tokens{1})];
end

% inputting city from user
cityName = input('What city are you interested in? ', 's');
% getting the index position from cities array
index = find(contains(cities, lower(cityName)));
% chacking whether city exist or not
if index ~= -1
% if yes then get citiy's latitude with that index
cityLat = latitude(index);
% getting the city type using sub-function
cityType = getRegionType(cityLat);
% printing
fprintf('%s is in a %s region\n', cityName, cityType)
else
% else if city not found then print this error
fprintf('City not found!\n')
end


% Sub-Function To read file and store the contents in a cell
% array and return it
function [data] = readFile(filename)
fid = fopen(filename, 'r');
if fid == -1
error(['Error! file ''', filename, ''' not found'])
end

data = textscan(fid, '%s%s%s%s%s', 'HeaderLines', 1, 'delimiter', '\t');
end

% Sub-Function to calculate and return the region
% type
function [type] = getRegionType(lat)
type = 'NA';
if lat > 66
type = 'Polar';
elseif (lat > 35 && lat <= 66)
type = 'Temperate';
elseif (lat >= 0 && lat <= 35)
type = 'Tropical';
end
end

output


Related Solutions

Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they would like to enter a real number. If yes, prompt the User for the real number. Continue to do this until the User enters “no” to the first question. After the User enters “no”, display the average of all the numbers entered. (Using Matlab) and "while" function 2.   Write a program that prompts the User for if they would like to enter a real...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB...
Write a user defined MATLAB program that performs power factor correction. The inputs to the MATLAB function should be voltage across the load (in Vrms, assume 0 phase), frequency Resistance of the load Inductance of the load power factor of the load target power factor The output of the function should be the size of the capacitor that one would need to place in parallel with the load to reach the target power factor. Please submit the code for the...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number...
Write a MATLAB program to do the following: 1- Allows the user to enter unlimited number of data set. 2- Allows the user to exit the program at any time by entering zero. 3- Calculates and displays the following statistics for the entered data set: a- Count of positive, negative, and total numbers. b- Maximum and Minimum numbers. c- Sum and Average of positive numbers. d- Sum and Average of negative numbers. e- Overall Sum and overall average of all...
3. [1 pts] Part A. Write a program for the GW Admissions Office. a) Request the...
3. [1 pts] Part A. Write a program for the GW Admissions Office. a) Request the staff to enter a string, to indicate someone’s NetID. b) Generates an email address, by appending “@gwmail.gwu.edu” to the user input c) Print the generated email account [2 pts] Part B. Write a program to meet additional requirements. In addition to requirements in Part A, your function should perform the following check: a) If the user input is longer than 10 characters (11 or...
11. Write a user oriented MATLAB program that will calculate the capacitance of the capacitor connected...
11. Write a user oriented MATLAB program that will calculate the capacitance of the capacitor connected across the loads to improve the overall power factor to 0.8 lagging,0.9 lagging, unity power factor, 0.8 leading, 0.9 leading. Also MATLAB will calculate total reactive, real power, and the total current at the source for each ste
MATLAB Write a script which asks the user of the program to provide an initial horizontal...
MATLAB Write a script which asks the user of the program to provide an initial horizontal position, initial vertical position, initial velocity, and angle. Create a time vector spanning from zero seconds to 100 seconds incremented at 0.01 seconds. Call the function that you created in the previous problem to calculate the trajectory and velocities of the projectile. Find the maximum height of the projectile and the time at which it reaches that point. Write a neat sentence stating what...
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes...
1. (50 pts) Write a C program that generates a 2D array-of-double and finds the indexes of the largest value stored in the 2D array. Specific requirements: (1) Your main function defines the 2D array a. You will need to prompt the user to specify the size of the 2D array b. You will need to prompt the user to put in numbers to initialize the array (2) Write a function to display the array that is visualized as rows...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT