Question
using matlab
3. Write a script that will read the ages vector.dat file created in the previous problem. - the script will print the number
0 0
Add a comment Improve this question Transcribed image text
Answer #1

% Matlab script to read a file containing ages of a group of people and
% then determine the number of people in the group, the maximum, minimum
% and average age of people in the group and then plot a graph of the ages

% open the file
fid = fopen('ages_vector.dat');
% read the ages into the vector ages ([1,inf] means each row contains 1
% data for age and inf is to read till the end of file)
ages = fscanf(fid,'%d',[1,inf])';
fclose(fid); %close the file

% display the statistics
fprintf('The file contains the ages of %d people\n',length(ages));
fprintf('The maximum age and minimum age in the group are %d and %d\n',max(ages),min(ages));
fprintf('The average age is %.2f years old\n',mean(ages));
graph_function(ages); % call the function to plot the graph


function graph_function(ages)
% Matlab function that receives as input the ages vector and plots the
% age of each people and the average age
ages = round(ages); % round the ages of the people
bar(ages); % plot the age of each person
hold on; % hold the current graph
% plot the average age
plot((1:length(ages)),mean(ages)*ones(1,length(ages)),'r+');
hold off; % release the current graph
legend('age','average'); % specify the legend
% specify the x-axis and y-axis labels
xlabel('each one of the person in the group');
ylabel('corresponding age');
% specify the title
title('The age of each person in the group');
end
%end of function

%end of script

Output:

Input file:

Each line in the input file contains 1 age

30 25 18 26 12 38 34 19 26 29 23 18 27 30 25 16 10 23 35 20 18 19 15 12 38 35

NEW WNNNW WWEHENENNWNNWN oo www o o o Віл о л о оо оо л шоо олоо

Output:

The file contains the ages of 50 people The maximum age and minimum age in the group are 38 and 10 The average age is 24.38 y

Add a comment
Know the answer?
Add Answer to:
using matlab 3. Write a script that will read the ages vector.dat file created in the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a script that will create a (random) vector of ages (real numbers, not integers). The...

    Write a script that will create a (random) vector of ages (real numbers, not integers). The script should contain the following prompts: - the user will provide the number of elements in the vector. - the user will provide the age of the oldest person in the group. For example: Enter the number of persons in the group: 50 Enter the age of the oldest person: 37 Save the vector containing the ages in a “.dat file” called ages_vector.dat matlab...

  • Using MATLAB. Create a script file that calls a user-defined function within a for/end loop to...

    Using MATLAB. Create a script file that calls a user-defined function within a for/end loop to symbolically compute the derivative of ln(x) on the interval from -5 to 5, containing 100 data points. Your script file should output a plot of the derivative of ln(x) (taken from your function) vs x. Use a line with circular markers for the plot and include a grid and appropriate labels. The user-defined function should 1. Receive a single x value as input. 2....

  • 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a =...

    Only calculate manually 1. Write a short MATLAB script or function (m-file) that generates the functions (t)eu(t) for a = 0.1. Plot the value of x(t). Since the e-af almost attenuates to 0 after 5T, where T1. So make your time vector from 0 to 5T would be enough to capture the signal.) 2. Using the definition of the FT (1), manually calculate the FT of the given functions in previous exercise 0O x(t)e 3. Plot the absolute values and...

  • MATLAB Question: Write a script for the problem pictured. Please send the script, not just the...

    MATLAB Question: Write a script for the problem pictured. Please send the script, not just the printout. Make sure your script allows you to input a value for n so when prompted, in the command window, user types n value. The n value should be the f_k if k ==1 .... Please also send print outs if you can. BELOW IS A SAMPLE ANSWER THAT DID NOT PUT THE INPUT SCRIPT NEEDED FOR THIS QUESTION TO BE CORRECT. YOU CAN...

  • matlab The Homework should be submitted as a script (.m) file Write a script areaMenu that...

    matlab The Homework should be submitted as a script (.m) file Write a script areaMenu that will print a menu consisting of "Cylinder," "Circle," and "Rectangle." It waits for the user to choose from the menu then call the corresponding function of each choice as follows: 1- Cylinder will call the function (cyl) which prompts the user for the radius and height of the cylinder, then calculates and prints area of the cylinder. 2- Circle will call the function (cir)...

  • Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates...

    Solve in Matlab by using Script file: Problem 4: Write a user-defined MATLAB unction that calculates the determinant of a 3x3 matrix by using the formula: A22 A23 11 A32 A33 A23 A12 A31 A33 21 A For the function name and arguments use d3-det3by3 (A), where the input argument A is the matrix and the output argument d3 is the value of the determinant. Write the code of det3by3 such that it has a subfunction that calculates the 2x2...

  • DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the...

    DO THIS IN MATLAB PLEASE DO THIS IN MATLAB Create a script file that performs the following calculations. Your script will use the functions you create. Label all graphs appropriately. For this project, do not have your homemade functions fprintf anything, instead have all your fprintf commands within your script. Attach your published script file along with .m files for your functions. Exercise 1. A fundamental iterative method for finding the roots of equations of one-variable is known as Newton's...

  • Using Matlab or Octave, I do not know how to write the script for this problem,...

    Using Matlab or Octave, I do not know how to write the script for this problem, please help. Must match sample output 5. Consider a 3 x 3 matrix given by A = 01 012 013 az az 633 ( dai 632 033 The trace of A is defined as tr(A)=2au. Given another 3 x 3 matrix bu bı2 bia B- bi brz bra | bgi bz2 by it can be shown that trAB') = tr(AB). Write a script that...

  • USING MATLAB: Please show how to do this in Matlab a) Create a script file that...

    USING MATLAB: Please show how to do this in Matlab a) Create a script file that uses three sets of nested loops to calculate the final temperature of a light bulb filament. The three nested loops are necessary to calculate Tf for all possible combinations of P, A, and Ti for the engineering formula below. The output from the script should be a 1xn vector representing the values of Tf. You must initialize the output variable so it does not...

  • MATLAB HW 11 problem using Switch Case and Input commands Write a script file that calculates...

    MATLAB HW 11 problem using Switch Case and Input commands Write a script file that calculates the cost of a movie tickets according to the following price schedule:                                                  Number of Tickets being purchased Time of Movie < =5 tickets 6 to 9 tickets >=10 tickets Afternoon $6 $30 + $5 for the number of tickets above 5 $50 + $4 for the number of tickets above 9 Weeknight $7 $35 + $6 for the number of tickets above 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT