User Defined Functions in MATLAB

In summary, the function GPA calculates the grade point average on a scale of 0 to 4, where A = 4, B = 3, C = 3, D = 1, and E = 0. The function is designed to be used with a vector of characters as input, and it works correctly if the input vector is properly formatted.
  • #1
samuel1994
3
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
  • #2
Post whatever code you've already done so I can take a look at where you're going with it.
 
  • #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.
 
  • #4
Have you tried using str2num?

A = str2num ('4')
 
  • #5
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.
 
  • #6
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'.
 
  • #7
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.
 
  • #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)
 
  • #9
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.
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.
 

What is a user defined function in MATLAB?

A user defined function in MATLAB is a custom-made function that is created by the user to perform a specific task or calculation. It allows the user to write their own code and save it for future use, making their MATLAB code more efficient and organized.

How do you create a user defined function in MATLAB?

To create a user defined function in MATLAB, you first need to define the function name and input variables. Then, write the code for the function inside a separate file with the same name as the function. Finally, save the file in the current directory or add the directory to the MATLAB path.

What are the benefits of using user defined functions in MATLAB?

There are several benefits of using user defined functions in MATLAB. They help in organizing and simplifying code, making it easier to debug and maintain. They also allow for code reuse, as the same function can be used multiple times in a program. Additionally, user defined functions can improve the efficiency and speed of code execution.

How do you call a user defined function in MATLAB?

To call a user defined function in MATLAB, you simply need to use the function name and input the required parameters, if any. The function will then execute the code and return the output. Make sure to add the directory where the function is saved to the MATLAB path, if it is not already in the current directory.

Can user defined functions be nested in MATLAB?

Yes, user defined functions can be nested in MATLAB. This means that a function can call another user defined function within its own code. This allows for more complex and modular code to be written, making it easier to understand and maintain.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
951
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
Back
Top