1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

User Defined Functions in MATLAB

  1. Oct 12, 2013 #1
    1. The problem statement, all variables and given/known data

    Write a user-defined function that calculates grade point average (GPA) on a scale of 0 to 4, where A = 4, B = 3, C = 3, D = 1, and E = 0. For the function name and arguments use av = GPA(g,h). The input argument g is a vector whose elements are letter grades A, B, C, D, or E entered as strings. The input argument h is a vector with the corresponding credit hours. The output argument av is the calculated GPA. Use the function to calculate the GPA for a student with the following record:

    Grade B A C E A B D B
    Credit Hours 3 4 3 4 3 4 3 2

    For this case the input arguments are: g=[‘BACEABDB’] and h=[3 4 3 4 3 4 3 2].


    2. Relevant equations

    None.

    3. The attempt at a solution

    I will be honest here, I have not attempted a solution past the point of defining a function with the output av, called the function GPA and with input arguments g and h. This is because I'm not entirely sure how to go from the string that would be inputted into numbers that should be in the GPA function. For example, I understand that B should be equivalent to 3, A should be 4, etc. However, how do I go from the string to numbers? I have tried defining each of the variables, A, B, C, D, and E after the function is defined, however I get errors if I do this.
     
  2. jcsd
  3. Oct 12, 2013 #2

    Student100

    User Avatar
    Education Advisor
    Gold Member

    Post whatever code you've already done so I can take a look at where you're going with it.
     
  4. Oct 12, 2013 #3
    function av = GPA(g,h)
    A = 4; B = 3; C = 3; D = 1; E = 0;
    av = sum(g)/sum(h)

    This is what I have. I know it's wrong, but I'm merely lost on how to convert the string into numbers.
     
  5. Oct 12, 2013 #4

    Student100

    User Avatar
    Education Advisor
    Gold Member

    Have you tried using str2num?

    A = str2num ('4')
     
  6. Oct 12, 2013 #5

    Mark44

    Staff: Mentor

    The input arguments g and h are vectors, or arrays. You need to look for some examples of how to get or set the j-th element of a vector.

    You also need to look for some examples that show how to use an if ... else if structure. This is the most obvious way of taking a character value in the g array and converting it to a number. There are other, shorter ways, but they're not as obvious.

    How do you find out what you have available to work with? Do you have a textbook that explains matlab syntax, or do you use notes from the instructor? The company that markets matlab has a nice website with all sorts of documentation on how to use their product. Here's a link to a page with lots of links to tutorials and documentation - http://www.mathworks.com/academia/student_center/tutorials/launchpad.html.
     
  7. Oct 12, 2013 #6

    Mark44

    Staff: Mentor

    I don't see how this would be helpful. The OP has an input vector of characters such as 'A', 'B' and the like. All that your example would do is store the number 4 in the A variable. It wouldn't work if used to convert, say 'B'.
     
  8. Oct 12, 2013 #7

    Student100

    User Avatar
    Education Advisor
    Gold Member

    Yeah, you’re absolutely right.
     
  9. Oct 12, 2013 #8
    Thank you for you help. I figured out the problem. This was my final code:

    function av = GPA(g,h)
    for k = 1:length(g)
    if g(k) == 'A'
    g(k) = 4;
    elseif g(k) == 'B'
    g(k) = 3;
    elseif g(k) == 'C'
    g(k) = 2;
    elseif g(k) == 'D'
    g(k) = 1;
    elseif g(k) == 'E'
    g(k) = 0;
    end
    end
    av = sum((g.*h))/sum(h)
     
  10. Oct 12, 2013 #9

    Mark44

    Staff: Mentor

    If you put a [code] tag at the start and a [/code] tag at the end, it will preserve your indentation, making your code a little easier to read. I've done this below.
    This looks about right. You can verify that it's working correctly by doing the GPA calculation manually and comparing that with the result from your code. If both are the same, that's a good sign.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: User Defined Functions in MATLAB
Loading...