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.
**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.