Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

As you can see the correct syntax for creating a funcion is:

function return_value = function_name(parameter_1, parameter_2,...)

...

code...

return_value = value

In MATLAB you do not need to have a function description but it is good programming practice to add a comment on each function that describes what the function does. Also, if you add a description of the function MATLAB will be able to index it and return a description of your function if you type help function_name in the Command Window.

Once you have created a function you MUST name the .m file with the same name the function has. Otherwise you MATLAB will not be able to access your function when you call it.

We will start Start by creating the bending stress function that outputs σx given (M, ri, ro ).


 
Save this as bending_stress.m, which you can notice is the same name as the function name.  Make a copy of beam3.m using Save As ...  and call the new file beam5beam4.m

Comment outIn this file comment out the lines below


 Call function bending_stress Image Removed
Run and check output

Now we will need to call the function {{bending_press }}we just created to obtain the value for σx that is needed to create the plot. Do this by typing:

Image Added


Run  the file beam4.m and check the output  You should get the same plot you got in the file beam3.m

Before we part, let's remind ourselves of some important programming guidelines that we have followed in this tour:

...