views:

112

answers:

1

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.

+3  A: 

I am not going to solve your homework.. Instead, study the following example:

f = @(x) sin(x);
x = 0:0.1:2*pi;
plot(x, f(x))
zeroSol = fzero(f, 3);
set(gca, 'XTick',unique([0:2:6 zeroSol]))

alt text


EDIT

As suggested in the comment by @gnovice, to build a piecewise anonymous function, we can do the following:

f1 = @(x) x-10;
f2 = @(x) x+10;
f = @(x) f1(x).*(x<=0) + f2(x).*(x>0)

x = -1:0.1:1;
plot(x, f(x))

which represent a function f defined as:

f(x) = { x-10   , x<=0
       { x+10   , x>0
Amro
+1 for not solving the homework, but providing a useful example to help him learn.
James
@Amro: How do you directly insert the figure window as snap shot without saving as an image?
Harpreet
@Harpreet: like you said its just a screenshot. There's a ton of apps out there for the job, I personally use Greenshot http://sourceforge.net/projects/greenshot/
Amro
Ok. Thanks. I was just thinking that SO has some capability of directly integrating the figure with the code :)
Harpreet

related questions