Consider the function f
with branched definition
f(x)= x − 2x^2/3 for 0 ≤ x ≤ 1;
x − 2 − cos(πx) for 1 < x ≤ 3.
Write an anonymous function for f
and Plot the graph of the function over the interval to allow you to estimate its zeros.
Use these estimates in the FZERO command to find the positive zeros (that is, the positive values of x at which the function equals zero).
On the plot of the function set the Xtick marks to include the zeros and the integer values in the interval.
Remember that you should use the function handle of an anonymous function when you apply the command fzero.
In setting XTICK and YTICK the list has to consist of numbers in increasing order. You can first list the values in any order, for example, a = [0:3, zero1, zero2]
and then use the SORT command. That is, replace a
by sort(a)
.
The only difficulty left is that SORT does not eliminate repeats and no repeats are allowed are allowed in the list. Use instead the command UNIQUE. Replacing a
by unique(a)
sorts the elements of a
and eliminates the repeats.