What is the best kind of MATLAB function to teach beginners?
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.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. 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. functions, element-wise operators MATLAB Answers — New Questions