-7

The statement of the exercise is this one:

enter image description here

And the solutions is this one:

enter image description here

I can not understand how the function was evaluated for f=2 and f=3. Why is it evaluated as f(21) and not f(2)?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 3
    Please don't post images of code, post the code here. – Kami Kaze Apr 09 '18 at 08:33
  • 1
    @KamiKaze I agree with the sentiment, although I rejected your suggested edit for the following reasons: A) it removes valid tags from the question (the question is about math, functions and evaluation of functions), and whilst you did transcribe the code, you didn't copy the question statement of the 'task', which is required information for understanding the two code snippets. Please don't remove valid tags, and if you choose to transcribe an image, transcribe everything as the confusion might stem from any part of the image, not just the piece you transcribed. – Adriaan Apr 09 '18 at 08:51
  • @Adriaan I am sorry I didn't recognise the problem statement was also in the picture. (which is even worse). Still ALL the the tags removed had nothing to do with the problem presented. it was just about matlab syntax and not a mathematical problem. function is a wrong tag because `f`is not a function (cos would be but then again the problem here is not about cos). evaluation is a dead tag. – Kami Kaze Apr 09 '18 at 09:00

2 Answers2

1

linspace(a,b) creates an array spanning from a to b in 100 points, so linspace(0,3) creates an array [0/33 1/33 2/33, ... 98/33 99/33] whereas

x = 0:0.1:3

creates an array [0 0.1 0.2 0.3 ... 2.9 3], i.e. from 0 in steps of 0.1 to 3.

f = x.^3.*cos(x+1)

then calculates for each value contained in the array x the value of xi^3*cos(xi+1), where xi is the i-th element. So f will also have 31 elements. The 21st element, which is 2, will evaluate the function for f = 2^3*cos(2+1).

To explicitly show this you could use

f(x==2)

which will give you the same answer as f(21). Numerical equality not guaranteed, use abs(x-2)<eps or similar for stability


Note that you could have evaluated the function on x=2 and x=3 as well using the 'old' version of x, since x(67)==2 and x(100)==3.

An alternative to create the array with steps of 0.1 is using linspace(0,3,31), which creates a linearly spaced array starting at 0, ending at 3 and having 31 equally spaced steps. This is useful when you want a specific amount of steps instead of a specific step size, so in the case of this example I'd go with the colon notation indeed.

The dot in front of the power and multiplication functions, ^ and *, makes MATLAB evaluate those element wise, meaning that for all elements in x the function is evaluated. Omitting those dots would make MATLAB use ^ and * as matrix operations.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • The interesting thing here is that x==2 will work, as will x==3, ==4 and so on - with the code as above; or when the step is 0.01 and so on. I haven't tried everything, but I didn't manage to break it. On the other hand, x==0.7 and similar won't always work. – Zizy Archer Apr 09 '18 at 09:45
  • @ZizyArcher see [this question](https://stackoverflow.com/q/686439/5211833); you break numerical precision. Why the integers work whilst `0.7` doesn't is beyond my comprehension, but it boils down to that. – Adriaan Apr 09 '18 at 09:50
  • 1
    `linspace` by default chooses the number of steps to be 100, so `linspace(0,3)` would create an array of 100 points equally spaced between 0 to 3, not 4 points as was originally stated in your post (i.e. `[0 1 2 3]`). – rayryeng Apr 09 '18 at 14:23
0

The line:

x=0:0.1:3

Creates a vector from 0 to 3, in jumps of 0.1, as so:

0,0.1,0.2,...,1.9,2.0,...3.0

Where the value of 2.0 is at index 21. So in order to deduce the value of f(2), you actually want to check the output of the function f(x) for the value at index 21, i.e f(21)

Prostagma
  • 1,756
  • 9
  • 21