What is the best kind of MATLAB function to teach beginners?

9 Ansichten (letzte 30 Tage)
Jon
Jon am 15 Jan. 2025
Kommentiert: Jon am 18 Jan. 2025
Several releases ago (R2015a?), I would introduce students to MATLAB by getting them to plot graphs of simple functions. I would give fplot('sin(x)', [-10,10]) as an example and then ask them to plot . They would type fplot('x^3', [-10,10]) and get a message about needing to use element-wise operators. That was constructive advice which could be explained by saying that .^ is an element-wise operator.
Then character-array functions were deprecated. I changed my example to fplot(@(x) sin(x), [-10,10]), they now type fplot(@(x) x^3, [-10,10]) and get the warning "Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments". That is quite hard to explain, partly because our course has not yet covered vectors.
My question is: Is there a better way to introduce functions? Perhaps by using symbolic functions. MATLAB is continually introducing new kinds of functions (anonymous, local, nested, ...) and I want to avoid teaching obsolete forms.
  2 Kommentare
Stephen23
Stephen23 am 15 Jan. 2025
Bearbeitet: Stephen23 am 15 Jan. 2025
"That is quite hard to explain, partly because our course has not yet covered vectors."
Simple solution: introduce vectors and matrices first. Which they should be, because they are the fundamental building block of MATLAB (whose name comes from "MATrix LABoratory"). Just as TMW does:
I doubt that you will find many users on this forum who would recommend teaching MATLAB by avoiding its first class numeric data types and jumping into a rather special toolbox which behaves fundamentally differently from numeric computations using vectors, matrices and arrays (which MATLAB itself is actually based on).
Jon
Jon am 18 Jan. 2025
I think @Stephen23 assumes that my aim is to teach MATLAB. I would say that the overall aim is to teach various mathematical topics, using MATLAB as a tool where it is convenient. And students will thus be exposed to various MATLAB features. The ordering of topics depends on the prior knowledge of the students. In England most 18-year olds are familiar with functions of one variable so they come at the start of our course. But matrices only appear in the curriculum of A-level Further Maths which only a few students will have taken, and so we cover vectors and matrices at a later date.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 16 Jan. 2025
I think I agree with @Stephen23, in that teaching symbolics first can cause issues. We see many new users who think everything should be symbolic. If you don't know what something is, then define it as symbolic. The thing is, then they get hung up on how to use any numerical tool, since everything they have is in symbolic form.
Instead, I think it makes more sense to teach them concepts like a function hande, as you have been doing. Teach them how to plot a function, as you did. Then teach them about numerical tools like fzero, fminbnd, as ways to locate a solution to a problem. That plot will show them how to find a graphical solution to the same problems.
By teaching them about function handles, they also learn the beginnings of how to use and then write functions, and m-file functions. In the end, learning about functions is one of the most important things a user can learn.
  1 Kommentar
Jon
Jon am 18 Jan. 2025
The encourgement to stick with handles to anonymous functions is helpful.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 15 Jan. 2025
Anonymous functions have been supported since R12.1 if I recall correctly.
Nested functions have been supported since R14.
local functions have been supported since... I don't know, must have been before release 5.0
The only form of function that has ever been deprecated in MATLAB is the use of inline which started to be depreciated as soon as anonymous functions were introduced in R12.1
That said, the Symbolic Toolbox used to accept character vectors in a number of circumstances that are not currently accepted, with the transition being about R2009a to R2011b.
Don't worry so much about teaching obsolete forms, as long as you avoid quoted strings for symbolic expressions and avoid using inline()
  1 Kommentar
Steven Lord
Steven Lord am 16 Jan. 2025
Function handles were introduced in release R12 (MATLAB 6.0).
Anonymous and nested functions were introduced in release R14 (MATLAB 7.0).

Melden Sie sich an, um zu kommentieren.


Andrew Frane
Andrew Frane am 16 Jan. 2025
You say your course hasn't yet covered vectors. But in your example, you use [-10, 10], which is a vector. And in your explanation to students you refer to "element-wise operators," which is a meaningless concept if vectors haven't been introduced (what is an "element" after all?).
I agree with the other responders who say it makes much more sense to introduce vectors very early on in the course—certainly before you try to introduce functions that take vectors as inputs and discuss elementwise operations! I typically introduce vectors basically immediately after introducing variables. The idea that you can say something like testScores = [80 72 94] to define a series of three test scores is a pretty straightforward extension of the idea that you can say something like testScore = 80 to define a single test score.
Regarding how to introduce defining a function, why not start with anonymous functions, which are the simplest, since they are just a type of variable? For example, here's a simple anonymous function to convert degrees fahrenheit to degrees celcius:
f2c = @(degreesF) (degreesF - 32) * 5/9
I typically start even simpler. Here's a function that doubles the input and adds one:
doublePlusOne = @(x) 2*x + 1
  1 Kommentar
Jon
Jon am 18 Jan. 2025
You and I know that [-10 10] is a vector. But it is not necessary to introduce that straight away. I can just say that fplot(@(x) ..., [-10,10]) is the syntax to plot a function for values of x from -10 to 10. [It is quite common to teach computer programming without all the complications; for example in a first (Hello World) Java program public static void main (String[] args) can be explained as a magic incantation that will be explained later.]
Your suggestion to start with simpler anonymous functions is helpful.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by