Create script matlab

  • MATLAB
  • Thread starter DryRun
  • Start date
  • #1
DryRun
Gold Member
838
4

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
188
1
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
DryRun
Gold Member
838
4
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
188
1
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:
 

Related Threads on Create script matlab

  • Last Post
Replies
1
Views
2K
Replies
3
Views
4K
Replies
6
Views
2K
Replies
3
Views
6K
Replies
4
Views
3K
Replies
2
Views
9K
Replies
4
Views
2K
Replies
2
Views
8K
Replies
2
Views
7K
Top