Calculate Grades and Pass/Fail Status with Basic Matlab Programming

In summary, the conversation was about a script to calculate a student's grade and pass/fail status based on their marks. The script uses an 'if' loop with 'elseif' statements to compare the final mark with different ranges and display the corresponding grade and pass/fail status. However, the original code did not have an 'end' statement to terminate the first 'if' loop and used relational operators in an invalid way. Restructuring the code with separate 'elseif' statements for each comparison solved the problem.
  • #1
roam
1,271
12
http://img16.imageshack.us/img16/3078/15688239.gif

So here's my script:

Code:
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:
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:
   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:
Physics news on Phys.org
  • #2
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:
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:
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
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/if.html
 
Last edited by a moderator:
  • #3
Thanks a lot for your input, I tried it and it worked. :smile: :smile:
 

1. How do I calculate grades using Matlab?

To calculate grades using Matlab, you can use the built-in functions such as sum, mean, and max to calculate the total points, average, and maximum points for each assignment or exam. Then, you can use conditional statements to assign letter grades based on the overall score.

2. Can I use Matlab to calculate pass/fail status?

Yes, you can use Matlab to calculate pass/fail status by setting a passing grade threshold and using conditional statements to determine if the overall score is above or below the threshold.

3. How do I input grades into Matlab?

To input grades into Matlab, you can use the input function to prompt the user for input and store the grades in variables. Alternatively, you can read grades from a file using the readmatrix function.

4. Can I use Matlab to calculate grades for multiple students?

Yes, you can use Matlab to calculate grades for multiple students by using arrays or matrices to store the grades for each student. Then, you can use loops to perform calculations and assign grades for each student.

5. Is it possible to customize the grading criteria in Matlab?

Yes, you can customize the grading criteria in Matlab by adjusting the passing grade threshold or using different conditional statements to assign letter grades. You can also create your own grading scale and use it in your calculations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Precalculus Mathematics Homework Help
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • STEM Academic Advising
Replies
9
Views
21K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
Back
Top