MatLab Program Project
MatLab Program Project
**Begin with typing in the Live Script:
format Comment by Wadlington, Julie: I do not see this at all. Just copy paste it in the code
format compact
syms x
F = @(x) atan(x) + x – 1
F1 = eval([‘@(x)’ char(diff(F(x)))])
G=@(x) x.^3-x-1
G1=eval([‘@(x)’ char(diff(G(x)))])
Notes: (1) a MATLAB command syms x defines a symbolic variable x;
(2) F and G are the function handles and F1 and G1 are the function handles for the first derivatives of F and G, respectively.
**Next, we create and output 2-D plots of the functions F, G, and y=0 on the interval [-2, 2] in order to visualize the x-intercepts and choose initial approximations. An initial value should be chosen close to the x-intercept which we are approximating. Comment by Wadlington, Julie: I see a graph I see F I do not see G, y=0 [-2,2]
**Type in the Live Script the code given below – it will output the graphs of F and G together with the function y=0 after you Run the section.
yzero=@(x) 0.*x.^(0)
x=linspace(-2,2); Comment by Wadlington, Julie: Copy past this code in after the top code
plot(x,F(x),x,yzero(x));
plot(x,G(x),x,yzero(x));
Note: here we have created another symbolic function yzero.
It is obvious from the properties of the function F(x) that it has only one real zero, which we will approximate. Concerning the function G, which is a polynomial of the third degree (we will denote it p), we are going to verify that it has only one real zero. In order to do that, we find all zeros of the polynomial p using MATLAB built-in functions sym2poly and roots: the function sym2poly(p) outputs the vector of the coefficients of the polynomial p (in descending order according to the degree), and the composition of two functions roots(sym2poly(p))outputs all zeros of the polynomial p. Comment by Wadlington, Julie: I don’t see this
**Type in the Live Script:
syms x
p=x^3-x-1;
roots(sym2poly(p))
After you Run Section, this part of the code will output the three zeros of the polynomial p – two of them are complex conjugate numbers and one is a real zero that we will approximate.
Next, we proceed with constructing a function in the file that approximates a real zero.
**Create a function called newtons. It begins with: Comment by Wadlington, Julie: Or this
function root=newtons(fun,dfun,x0)
format long
The inputs fun and dfun are the function and its first derivative, respectively, and x0 is the initial approximation. The output root will be our approximation of the real zero of a function. We will program consecutive iterations according to the Newton’s method, and we will assign to root the iteration which will be the first one falling within a margin of 10^(-12) from the MATLAB approximation x of that zero – the last is delivered by a built-in MATLAB function fzero. The details are below:
**Type the line
x=fzero(fun,x0) Comment by Wadlington, Julie: I do not see this
in your function newtons and output x with a message that it is a MATLAB approximation of the real zero of the function.
**Then, your function newtons will calculate consecutive iterations , using Newton’s Method (see Theory above). You can employ a “while loop” here. The loop will terminate when, for the first time, abs(-x)<10^(-12) for some consecutive iteration . Output with a corresponding message the number of iterations N, and assign the last iteration, , to the output root. This will be the end of your function newtons. Comment by Wadlington, Julie: Or this
**Print the function newtons in your Live Script.
**Next, proceed with the following tasks in the Live Script:
Part (a)
You will work with the function F in this part. Using the graph of the function F, choose three different values of the initial approximation x0 of the zero of F.
**Then, input the function handles:
fun=F;
dfun=F1;
and run the function root=newtons(fun,dfun,x0) for each of your three choices of x0 – one at a time.
Part (b)
You will work with the function G in this part.
**First, input the corresponding function handles.
**Then, run root=newtons(fun,dfun,x0) for each of the initial approximations (1)-(8) of the real zero of G:
(1) x0=1.3;
(2) x0=1;
(3) x0=0.6;
(4) x0=0.577351;
(5) Pick the initial value x0 to be the positive zero of the derivative of G(x). Type
x0=1/sqrt(3)
(display x0) and run the function root=newtons(fun,dfun,x0).
(6) x0=0.577;
(7) x0=0.4;
(8) x0=0.1;
1
0
x
,0:
n
xnN
=
N
x
N
x
N
x