Letβs put this all together in an example in MATLAB. In the script that you have already started, create a new section. If you forgot how to do this, a quick recap of Chapter 8 should help.
Now, Let us consider the function \(f(x)=x^3-6x^2+x-10\) and say that for whatever reason we would like to calculate the value of the function at different values of \(x\text{.}\) Specifically, lets consider evaluating the function at the following values: \(-3,-2,-1,0,1,2,3\text{.}\) Finally, letβs store all of the corresponding values of the evaluated function in a new array called fun.
The above βsolutionβ would be wasting the power of MATLAB and would be extremely tedious. Instead, think. about what we have learned and how it relates to this problem. Do not continue reading until you have some ideas about how to proceed.
I hope you didnβt skip your brain workout. It is important! I hope that you have some inclination that we can use arrays and array mathematics to solve this problem quickly.
Define the \(x\) array in MATLAB. Notice that the values of \(x\) are from β3 to 3 and are equally spaced by 1. Sound familiar? Sure! Just type in x =[-3:3] to define that vector.
To solve for the function at every different value of \(x\text{,}\) you just need to perform the mathematical operations onto the array of \(x\) like this:
For this question, letβs consider the preceding example, but this time, redefine x to be from x = 1 to x=999 equally spaced by 1. To accomplish this, you should only have to modify two numbers in your script. After running it, you have now created a fun array with 1000 entries!
That is the beauty of MATLAB! You can change one or two numbers, and re-run your script without having to do much work. If you struggled with the question above, it is ok! If you are struggling then you are learning! But you also need to figure out a plan to make sure that you learn this material. It is critical to being a successful MATLAB programmer.