MATLAB Create MATLAB Script: Average Voltage & Error Range

  • Thread starter Thread starter DryRun
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion focuses on creating a MATLAB script to calculate the average voltage and its error range based on user-inputted voltage readings. The initial script provided by the user is incomplete and lacks functionality for calculating the standard deviation. Suggestions include using MATLAB's built-in functions like 'mean' and 'std' for efficient calculations and organizing inputs into a vector. A more compact method for inputting multiple values at once is also discussed, with the recommendation to use a single input statement that can be converted into an array. The conversation emphasizes leveraging MATLAB's capabilities for handling arrays to simplify the coding process.
DryRun
Gold Member
Messages
837
Reaction score
4
Hi

I just started a course in learning how to use MATLAB and I'm asked to begin by writing some scripts.

This is the problem: A set of independent voltage measurements taken by four observers are recorded. Create a script that prompts the user to input the values of the readings and calculates the average voltage and its range of error.

Here is my script:
m1 = input ('Enter first value of voltage ');
m2 = input ('Enter second value of voltage ');
m3 = input ('Enter third value of voltage ');
m4 = input ('Enter fourth value of voltage ');
sum = m1 + m2 + m3 + m4;
average = sum/4

I know it's incomplete and maybe even incorrect, but i have no idea what to do.
 
Physics news on Phys.org
sharks said:
Hi

I just started a course in learning how to use MATLAB and I'm asked to begin by writing some scripts.

This is the problem: A set of independent voltage measurements taken by four observers are recorded. Create a script that prompts the user to input the values of the readings and calculates the average voltage and its range of error.

Here is my script:
m1 = input ('Enter first value of voltage ');
m2 = input ('Enter second value of voltage ');
m3 = input ('Enter third value of voltage ');
m4 = input ('Enter fourth value of voltage ');
sum = m1 + m2 + m3 + m4;
average = sum/4

I know it's incomplete and maybe even incorrect, but i have no idea what to do.
One of the big advantages of Matlab and other matrix-oriented applications, such as Mathcad and Mathematica, is that it has a lot of built-in functions that operate on entire sets (I mean Arrays not Sets) of data. This considerably simplifies problems such as the one you've been given. I'd look up how to put the data into a vector for a start and then look in the extensive and rather good help to see how to use such functions as 'mean' and 'std' (they're in the Data Analysis section of Functions in the User Guide).
 
Hi NemoReally

Thank you for your reply. I have implemented your suggestions. By "range of error", i assume the solution involves finding the standard deviation, but I'm not sure how to write it in a vector form, as I've only been able to write the inputs as an array. Also, is there a simpler or more compact method instead of writing the first 4 lines; a way to write the input line once and then request it 4 times for the 4 different m variables?

m1 = input ('Enter first value of voltage ');
m2 = input ('Enter second value of voltage ');
m3 = input ('Enter third value of voltage ');
m4 = input ('Enter fourth value of voltage ');
voltvec = [m1, m2 ,m3 ,m4];
average = mean (voltvec)
error = std(voltvec)
 
sharks said:
Hi NemoReally

Thank you for your reply. I have implemented your suggestions. By "range of error", i assume the solution involves finding the standard deviation, but I'm not sure how to write it in a vector form, as I've only been able to write the inputs as an array. Also, is there a simpler or more compact method instead of writing the first 4 lines; a way to write the input line once and then request it 4 times for the 4 different m variables?

m1 = input ('Enter first value of voltage ');
m2 = input ('Enter second value of voltage ');
m3 = input ('Enter third value of voltage ');
m4 = input ('Enter fourth value of voltage ');
voltvec = [m1, m2 ,m3 ,m4];
average = mean (voltvec)
error = std(voltvec)

Effectively, a vector is just another name for a one-dimensional array (some languages may introduce some subtleties around the distinction and I can't remember whether Matlab is one of those languages).

I believe, not being a Matlab user, that you should be able to enter all 4 values in one input, separated by commas or in Matlab array format; this will give you a string. You should then find the built-in function str2num will convert the string to an array, upon which you can standardly deviate to your heart's content! :smile:
 

Similar threads

Replies
2
Views
2K
Replies
2
Views
10K
Replies
3
Views
3K
Replies
12
Views
3K
Replies
25
Views
2K
Back
Top