User Defined Functions in MATLAB

  • Thread starter Thread starter samuel1994
  • Start date Start date
  • Tags Tags
    Functions Matlab
AI Thread Summary
The discussion revolves around creating a user-defined function in MATLAB to calculate the grade point average (GPA) based on letter grades and corresponding credit hours. The function, named GPA, takes two input arguments: a vector of letter grades and a vector of credit hours. Participants discuss the challenge of converting letter grades into numerical values, with suggestions to use conditional statements for this conversion. The final solution involves a loop that assigns numerical values to each letter grade and calculates the GPA by summing the products of grades and credit hours divided by the total credit hours. The importance of verifying the function's output against manual calculations is also emphasized.
samuel1994
Messages
3
Reaction score
0

Homework Statement



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].


Homework Equations



None.

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.
 
Physics news on Phys.org
Post whatever code you've already done so I can take a look at where you're going with it.
 
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.
 
Have you tried using str2num?

A = str2num ('4')
 
samuel1994 said:
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.

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.
 
Student100 said:
Have you tried using str2num?

A = str2num ('4')

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'.
 
Mark44 said:
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'.

Yeah, you’re absolutely right.
 
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)
 
If you put a [code[/color]] 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.
samuel1994 said:
Thank you for you help. I figured out the problem. This was my final code:
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)
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.
 

Similar threads

Replies
3
Views
1K
Replies
6
Views
4K
Replies
1
Views
283
Replies
3
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
10
Views
2K
Back
Top