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

Basic Matlab Programming

  1. Sep 15, 2009 #1
    http://img16.imageshack.us/img16/3078/15688239.gif [Broken]

    So here's my script:

    Code (Text):
    x=[a, b, c, d];
    w=[0.65 0.2 0.05 0.1]';
    final=x*w;
    disp(final);
    if 65>final>=50
        disp('Grade is: C')
    elseif 80>final>=65
        disp('Grade is: B')
    elseif final>=80
        disp('Grade is: A')
    If final>=50
        disp('Student Passes')
        else
            disp('Student Fails')
    end
    So for example I tried x = [70, 50, 30, 45]:

    Code (Text):
    x=[70, 50, 30, 45];
    w=[0.65 0.2 0.05 0.1]';
    final=x*w;
    disp(final);
    if 65>final>=50
        disp('Grade is: C')
    elseif 80>final>=65
        disp('Grade is: B')
    elseif final>=80
        disp('Grade is: A')
    If final>=50
        disp('Student Passes')
        else
            disp('Student Fails')
    end
    I got the following answer:

    Code (Text):
       61.5000

    Student Fails
    Well, it calculated the mark correctly, but I don't understand why it says "student fails" when it should be displaying "student passes". :confused: It also doesn't display the grade. What's the problem? Can anyone help? :uhh:
     
    Last edited by a moderator: May 4, 2017
  2. jcsd
  3. Sep 16, 2009 #2

    MATLABdude

    User Avatar
    Science Advisor

    Probably because you didn't terminate the first 'if' loop with an 'end' statement. My MATLAB may be a little rusty, but I also don't recall if you can use the relational operators in the way you do:
    Code (Text):
    if 65>final>=50
    You probably want to restructure with a bunch of elseif statements that make a single comparison and cascade downwards (such that you only get one output, and not all the outputs for which the final grade is valid, e.g. a mark of 90 getting you an A, B, C and fail):
    Code (Text):

    if final >= 80
          disp('Grade is: A')
    elseif grade >=65
          disp('Grade is: B')
    %other stuff
    end
     
    EDIT: Mathworks documentation pages for control statements, and the if statement:
    http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/brqy1c1-1.html#brqy1c1-3 [Broken]
    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/if.html
     
    Last edited by a moderator: May 4, 2017
  4. Sep 16, 2009 #3
    Thanks a lot for your input, I tried it and it worked. :smile: :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Basic Matlab Programming
  1. Matlab programming (Replies: 0)

  2. Matlab basics (Replies: 2)

  3. Programming in Matlab (Replies: 4)

  4. Basic MATLAB (Replies: 10)

Loading...