Create MATLAB Script: Average Voltage & Error Range

  • Context: MATLAB 
  • Thread starter Thread starter DryRun
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around creating a MATLAB script to calculate the average voltage and its range of error from a set of voltage measurements provided by users. Participants explore different methods for inputting data and calculating the average and standard deviation, focusing on MATLAB's capabilities.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant shares an initial script for calculating the average voltage from four user inputs but acknowledges its incompleteness.
  • Another participant suggests using MATLAB's built-in functions like 'mean' and 'std' for calculating the average and standard deviation, emphasizing the use of arrays.
  • A participant expresses uncertainty about how to implement the standard deviation calculation and seeks a more efficient way to gather user inputs without repeating code.
  • Further clarification is provided regarding the concept of vectors and arrays in MATLAB, with a suggestion to enter multiple values in a single input line.

Areas of Agreement / Disagreement

Participants generally agree on the utility of MATLAB's built-in functions for data analysis, but there is no consensus on the best method for inputting data or the exact implementation details of the script.

Contextual Notes

Participants mention potential methods for inputting data and calculating statistics but do not resolve the specifics of how to implement these methods in MATLAB.

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 4 ·
Replies
4
Views
7K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
10K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K