## Create Function-Type Problems

To help your learners master MATLAB® coding skills, you can write a problem that you want learners to solve using a function.

To write a function-type problem:

• To add a problem to a collection: On the Content tab, click Add Problem in the desired collection.

• To add a problem to a Course: On the Courses tab, click the course you want to add a problem to. On the assignment where you want the new problem, click Add Problem.

3. Select an existing problem from your own collection or from MathWorks® Collections or create your own problem starting with a blank template.

If you need additional help while creating a function-type problem, click the question mark icon next to the item for instructions.

Tip

At any time, you can click Choose Different Problem to abandon the draft, return to Add Problem, and make a new selection.

While drafting the coding problem, remember to save your work in progress. You have two options:

• — You can continue developing this problem while it is in draft mode.

• — The problem is complete. If this problem is part of a MATLAB Grader course you are developing, the problem is viewable to learners when the course is published. Learners can view only those problems that are in the Final state. If you want to remove a problem from learner view after publishing it, open the problem and then click .

To learn more about states, see Course, Assignment, and Problem States.

### Step 1. Describe Problem to Solve

1. Create a title and detailed problem description for the learner.

Using the rich text editor, you can include formatted text, equations, links, and images.

Note

When you write the problem description, clearly define the number and type of input variables and the number and type of expected outputs. Be specific about the types of values or units of measurement.

2. Optionally, provide data and code files that the reference solution, learner solution, and assessments can use. Add these items in Files Referenced.

3. Set Problem Type to Function. This setting tells MATLAB Grader that the learner solution must be formatted as a MATLAB function.

The following image shows the title and description for a function-type problem.

### Step 2. Write Problem Solution

Write a reference solution that you can then use to assess the learner solution.

• The Reference Solution is your solution. The learner cannot see this solution.

• The function must return at least one output. You can call the reference solution by name in your assessments and compare the output values to those returned by the learner function.

### Step 3. Write Code and Comments to Assist Learners

Provide a solution template for the learner.

• The Learner Template provides code that the learner starts with to write their solution. You decide how much code to provide to the learner.

• Click the lock icon to keep learners from changing code you provide.

• Use comments to provide coding instructions and prompts to learners.

• You must communicate to the learner what you expect them to name the function so that you can call it in your assessments. Consider locking the line with the function name in the learner template.

#### Preview Problem Display

To see how your content is formatted, click . The preview area displays the view that your learners see when solving the problem in MATLAB Grader.

You can interact with the to see assessment results, feedback, and error messages.

Learners also have the option of working on and submitting their solution using MATLAB Online™. They can code their solutions to MATLAB Grader problems using the workspace, Live Editor, and debugging features of MATLAB without having to download, install, or set up MATLAB.

To preview the problem as it will appear to learners in MATLAB Online:

1. Save the problem as final.

2. Click .

3. Click .

MATLAB Online displays the problem description, assessment and output tabs, and and buttons in a MATLAB Grader panel on the left side of your browser. As with other panels in MATLAB Online, learners can move the MATLAB Grader panel to the left or right side of the window.

You can learn more about the learner workflow in Submit Solution in MATLAB Online.

### Step 4. Create Assessments to Test Learner Solutions

Assessments for testing learner solutions to a function-type problem are more complex than those you write for script-type problems. This section provides a brief description of the UI elements. For more information about and examples for writing assessments for this type of problem, see Write Assessments for Function-Type Learner Solutions.

When a learner submits a solution, your assessments evaluate the learner solution. You can write as many tests as you think are necessary.

1. Specify the assessment method you want to use.

Under Assessment, select an assessment method.

• Correct/IncorrectMATLAB Grader treats the problem as pass/fail.

• Weighted — When creating multiple assessments for a single problem, you can choose to award partial credit for each test.

2. Code the assessment.

1. Click Add Assessment. MATLAB Grader numbers assessments automatically.

2. Provide a brief description of the test.

3. Select a test type.

• `Function or Keyword Is Present` — Provide the functions and keywords the learner must use in their solution.

• `Function or Keyword Is Absent` — Provide functions or keywords the learner must avoid using in their solution.

• `MATLAB Code` — Write assessments using MATLAB code. You can create inputs, call the reference and learner functions, and capture and determine correctness of the outputs. Use MATLAB code to combine multiple assessments into a single test.

Due to variable scope, assessments have access only to variables created by the test script.

3. Provide additional feedback to the learner.

MATLAB Grader provides default feedback if the solution fails the assessment. You can provide additional feedback to the learner using rich text format, including formatted text, hypertext links, images, and math equations.

4. Optionally, specify if the test is a pretest. A pretest is a test learners can run without submitting their solution.

1. By default, assessments are not pretests.

2. To make the assessment a pretest, select Pretest.

When you select to make an assessment a pretest:

• MATLAB Grader adds a button for the learner.

• When the learner clicks , MATLAB Grader runs the learner’s solution against the assessments marked as pretests and provides the instructor's feedback on those tests.

When writing pretests, keep in mind that learners can see the code used to assess pretests. You do not want to inadvertently include code needed to solve the problem in a pretest.

For assistance in how to write an assessment for a function-type problem, see Write Assessments for Function-Type Learner Solutions.

### Step 5. Write Code to Call Learner Function

Write a script that calls the learner function. This script runs when learners click .

• This field provides learners a way to test their functions before submitting their solution for assessment.

• Optionally, you can prepopulate this field with code for testing their function. Typically, this script creates representative inputs as variables and then calls the learner function with those inputs. MATLAB Grader displays the outputs in a meaningful way to the learner.

• This code does not run when learners click Submit.

### Step 6. Save Problem

When saving the coding problem, you have two options:

• — You can continue developing this problem while it is in draft mode.

• — The problem is complete. If this problem is part of a MATLAB Grader course you are developing, the problem is viewable to learners when the course is published. Learners can view only those problems that are in the Final state. If you want to remove a problem from learner view after publishing it, open the problem and then click .

To learn more about states, see Course, Assignment, and Problem States.

### Try an Example Function Problem

To get started with function-type problem authoring, try the example My first FUNCTION problem, which you can find in the MathWorks Collection Getting Started with MATLAB Grader.

To try solving the problem as a learner would, open the problem, click , and then click . Enter your code in the solution box and then click to run the assessments (when you perform this action in , it does not actually submit anything). Try both correct and incorrect code to see the feedback you get.

For more information about the problem solving workflow for learners, see Solution Submission Guide.