Question
Problem 1 Define a function in a file named stat.m in the MATLAB editor that returns the mean and standard deviation of an input vector.
Problem 1
Define a function in a file named stat.m in the MATLAB editor that returns the mean and standard deviation of an input vector.
Given: The function length() finds the maximum number of elements in an array
The function sum() sums up each element in an array
% Program Stat.m
values = [12.7, 45.4, 98.9, 26.6, 53.1]; [average,stdeviation] = stat(values)
% This is placed at the end of your MATLAB program % The function is called stat(x), it has one variable input 'x' and two outputs [m,s]
% This function calculates statistics functions of average and standard deviation function [m,s] = stat(x) n = length(x); m = sum(x)/n; s = sqrt(sum((x-m).^2/n)); end
- Run this program and find the values for the average and standard deviation
- Create a new vector of values = [9.9, 15.5, 29.2, 39.3, 45.5, 64.3, 87.1, 55.5] and use this new vector in your above program to calculate average and standard deviation
Problem 2. Create a small program called stat2.m that will have these two functions in at the end of the program. Verify that they produce the same result with:
values = [12.7, 45.4, 98.9, 26.6, 53.1];
values = [9.9, 15.5, 29.2, 39.3, 45.5, 64.3, 87.1, 55.5]
What is similar, and what is different about stat.m and stat2.m in what they actually do?
% Program Stat2.m
values = [12.7, 45.4, 98.9, 26.6, 53.1]; [average,stdeviation] = stat2(values)
function [m,s] = stat2(x)
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end
function m = avg(x,n)
m = sum(x)/n;
end
Print your results
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started