HW2 - Do I need to define a function?

Professor, regarding the first question of the homework which deals with the gaussian distribution, I defined  a variable to the actually function of the gaussian distribution and defined sigma and mu as constants, which are the two other variables in the equation. Am I going in the right direction or am I suppose to create a function?

JFF Answer:
you are going in the correct direction. You DO NOT need to define a function within matlab. Defining a function can save you space in terms of programming code, but it is not required for HW2. You can just define the variable. For example, here is some sample code (not a gaussian distribution, but an example of creating the plot which you need). For simplicity, I am using a linear function.

>> mu=3; sigma=6;
>> x=-5:0.1:5;
>> y1=mu*x+sigma;
>> mu=0; sigma=6;
>> y2=mu*x+sigma;
>> mu=1; sigma=3;
>> y3=mu*x+sigma;
>> plot(x,y1)
>> hold on
>> plot(x,y2)
>> plot(x,y3)
>> hold off

If you copy and paste the above code into MATLAB, the code should work. Note that I simply redefine mu and sigma three times and create a new 'Y' variable each time. Once I have accumulated the "Y" values for each mu and sigma values, I plot all three results on the same graph.

As you mentioned in your email, an alternative is to define a SINGLE function and then evaluate the function for different values of mu and sigma. See the code below.
 

(a) you first must create and save a script to define your function. The code for the function is

function [y]=JffFunction(x,mu,sigma)
y=mu*x+sigma;
end
 
(b) Once you save the script as JffFunction.m (matlab script), you can then call that function with the following code:

>> x=-5:0.1:5;
>> y1=JffFunction(x,3,6);
>> y2=JffFunction(x,0,6);
>> y3=JffFunction(x,1,3);
>> plot(x,y1)
>> hold on
>> plot(x,y2)
>> plot(x,y3)
>> hold off
>>
 
From a programming prospective, defining the function is 'better' because (a) it gives you fewer lines of code and (b) once you know that your function script is coded correctly, you don't have to worry about mistyping the formula any more... you just call a function which you KNOW works properly.
 
For the purpose of HW#2, you can use either method. however, eventually when you do a class 'project' at the end of the course, I would expect you to define functions as scripts.