Create MATLAB Script: Average Voltage & Error Range

  • MATLAB
  • Thread starter DryRun
  • Start date
  • Tags
    Matlab
In summary: Hi NemoReallyThank 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?
  • #1
DryRun
Gold Member
838
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
  • #2
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).
 
  • #3
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)
 
  • #4
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:
 
  • #5


Hi there,

Thank you for sharing your script and your question. It's great that you're starting to learn MATLAB and are already working on a problem like this.

First of all, let's break down the problem and think about what we need to do. We have four independent voltage measurements, and we want to calculate the average voltage and its range of error. This means we need to do the following steps:

1. Prompt the user to input the four voltage values
2. Store these values in variables
3. Calculate the sum of the four values
4. Calculate the average voltage by dividing the sum by 4
5. Calculate the range of error by finding the difference between the highest and lowest voltage values

Now, let's take a look at your script. You have correctly prompted the user to input the four voltage values and stored them in variables. However, you have not calculated the sum or the average voltage. Additionally, you have not calculated the range of error.

To calculate the sum, we can use the "sum" function in MATLAB. This function takes in an array of values and returns the sum of those values. In this case, we can create an array of the four voltage values and pass it to the "sum" function. The updated script would look like this:

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 ');
voltage_values = [m1, m2, m3, m4];
sum = sum(voltage_values);
average = sum/4;

Now, to calculate the range of error, we need to find the highest and lowest voltage values and then find the difference between them. We can use the "max" and "min" functions in MATLAB to find the highest and lowest values. The updated script would look like this:

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 ');
voltage_values = [m1, m2, m3, m4];
sum = sum(voltage_values);
average = sum/4;
max_voltage = max(voltage_values);
min_voltage = min(voltage_values);
range_of_error = max_voltage - min_voltage;

Now, we have calculated
 

1. What is a MATLAB script?

A MATLAB script is a file containing a series of MATLAB commands that can be executed together to perform a specific task or set of calculations.

2. How do I create a MATLAB script?

To create a MATLAB script, you can either open the MATLAB editor and type in your commands, or you can save a series of commands you have already entered in the command window as a script file.

3. What is the average voltage?

The average voltage is the mean value of a set of voltage measurements. It is calculated by summing all the voltage values and dividing by the total number of values.

4. What is the error range?

The error range, also known as the margin of error, is a measure of the uncertainty or variability in a set of data. It is often expressed as a percentage or a range of values above and below the mean.

5. How can I calculate the average voltage and error range using MATLAB?

To calculate the average voltage and error range using MATLAB, you can use the "mean" and "std" functions respectively. The mean function will return the average voltage, and the std function will return the standard deviation, which can be used to calculate the error range.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
9K
  • Programming and Computer Science
Replies
25
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
4
Views
2K
Back
Top