Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Create script matlab

  1. Jun 8, 2012 #1

    sharks

    User Avatar
    Gold Member

    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.
     
  2. jcsd
  3. Jun 8, 2012 #2
    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).
     
  4. Jun 8, 2012 #3

    sharks

    User Avatar
    Gold Member

    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)
     
  5. Jun 9, 2012 #4
    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:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Create script matlab
  1. Matlab - basic script (Replies: 1)

Loading...