Question

(20) Consider the function f(x) = e* - 3x. Using only and exactly the four points on the graph off with x-coordinates -1,0, 1

Please MATLAB for all coding with good commenting.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

All the explanation is in the code comments. Hope this helps!

Code:

% given function

f = @(x) exp(x) - 3*x;

% given x values

x = [-1 0 1 2];

% f(x) for x

y = f(x);

% use polyfit to approximate

p = polyfit(x, y, 3);

figure

hold on;

% plot the fuction and polynomial for range [-1, 2]

x = -1:0.1:2;

% f has blue dashed line

plot(x, f(x), 'b--')

% polynomial has red thick line

plot(x, polyval(p, x), 'r', 'LineWidth', 2)

xlabel('x')

% added legend at end to support all the marked points

legend('f(x)', 'polynomial')

% (a) use roots function for roots of polynomial

r = roots(p);

% plot as green triangles

plot(r, polyval(p, r), 'g^', 'MarkerFaceColor', 'g', 'DisplayName', 'roots of polynomial')

% (b)

r = fzero(f, 0);

% plot as big yellow triangle

plot(r, f(r), 'y^', 'MarkerSize', 10, 'DisplayName', 'root of function')

% (c) use newton raphson method to find root of f

x0 = 0;

% df/dx = e^x - 3

df = @(x) exp(x) - 3;

% loop till x3

for i=1:3

% next x

xn = x0 - f(x0)/df(x0);

% update x0 for next iteration

x0 = xn;

end

% report the x3, x3 = 0.6190 almost equal to r (= 0.6191)

xn

Sample output:

3.5 3 -f(x) polynomial roots of polynomial root of function 2.5 2 1.5 1 0.5 0 -0.5 -3 -2 0 1 2 -1 X

Note - the yellow coloured triangle is quite faint, it overlaps with the middle green triangle

xn = 0.6198

Code screenshots:

% given function f = @(x) exp(x) - 3*X; % given x values X = (-1 0 1 2]; % f(x) for x y = f(x); % use polyfit to approximate

% (C) use newton raphson method to find root of f x@ = e; % df/dx = e^x - 3 df = @(x) exp(x) - 3; % loop till x3 for i=1:3 %

Add a comment
Know the answer?
Add Answer to:
Please MATLAB for all coding with good commenting. (20) Consider the function f(x) = e* -...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In MATLAB please Consider the nonlinear function: y = f(x) = x3 cos x a. Plot...

    In MATLAB please Consider the nonlinear function: y = f(x) = x3 cos x a. Plot y as a function of x as x is varied between -67 and 67. In this plot mark all the locations of x where y = 0. Make sure to get all the roots in this range. You may need to zoom in to some areas of the plot. These locations are some of the roots of the above equation. b. Use the fzero...

  • 6) Use MATLAB and Newton-Raphson method to find the roots of the function, f(x) = x-exp...

    6) Use MATLAB and Newton-Raphson method to find the roots of the function, f(x) = x-exp (0.5x) and define the function as well as its derivative like so, fa@(x)x^2-exp(.5%), f primea@(x) 2*x-.5*x"exp(.5%) For each iteration, keep the x values and use 3 initial values between -10 & 10 to find more than one root. Plot each function for x with respect to the iteration #.

  • 4) (16 points) The function f(x)= x? – 2x² - 4x+8 has a double root at...

    4) (16 points) The function f(x)= x? – 2x² - 4x+8 has a double root at x = 2. Use a) the standard Newton-Raphson, b) the modified Newton-Raphson to solve for the root at x = 2. Compare the rate of convergence using an initial guess of Xo = 1,2. 5) (14 points) Determine the roots of the following simultaneous nonlinear equations using a) fixed-point iteration and b) the Newton-Raphson method: y=-x? +x+0,75 y + 5xy = r? Employ initial...

  • 1. This question concerns finding the roots of the scalar non-linear function f(x) = r2-1-sinx (1...

    1. This question concerns finding the roots of the scalar non-linear function f(x) = r2-1-sinx (1 mark) (b) Apply two iterations of the bisection method to f(x) 0 to find the positive root. (3 marks) (c) Apply two iterations of the Newton-Raphson method to find the positive root. Choose (3 marks) (d) Use the Newton-Raphson method and Matlab to find the positive root to 15 significant (3 marks) (a) Use Matlab to obtain a graph of the function that shows...

  • please show answer in full with explanation, also show matlab 1. Consider the function f(x)2.753 +18r2 21 12 a) Plot...

    please show answer in full with explanation, also show matlab 1. Consider the function f(x)2.753 +18r2 21 12 a) Plot the graph of f(x) in MATLAB and find all the roots of the function f(x) graphically. Provide the code and the plot you obtained. b) Compute by hand the first root of the function with the bisection method, on the interval -1; 0) for a stopping criterion of 1% c) How many iterations on the interval -1, 0 are needed...

  • Function driver and script file please 4) The polynomial f (x)-0.0074x*-0.284x3+ 3.355x2 12.183x +5 has a...

    Function driver and script file please 4) The polynomial f (x)-0.0074x*-0.284x3+ 3.355x2 12.183x +5 has a real root between 15 and 20. Apply the Newton-Raphson method to this function using an initial guess of xo-16.15. Explain your results. 5) Use the roots MATLAB function to find the roots of the polynomial x x-1-0. Compare your answer to the answer you derived in in question 1. 6) Write the following set of equations below in matrix form. Use MATLAB to solve...

  • 1a): Find the roots of the following polynomial equations using matlab: f(x) = x^5 − 9x^4...

    1a): Find the roots of the following polynomial equations using matlab: f(x) = x^5 − 9x^4 + 31x^3 − 51x^2 + 40x − 12 f(x) = x^5 − 11x^4 + 47x^3 − 97x^2 + 96x − 36 b): Find the coefficients of two polynomial equations in matlab given the following sets of roots: r1 = [ 100 25 50 5 10 ]    r2 = [ 11 22 33 0 55 ] c): Create a figure with four subplots, each showing...

  • Consider the function xtan x -1 defined over all x. Sketch the function to get an idea of the roots 1 find the first couple of roots using bisection to a precision of machine epsilon 2 after straddli...

    Consider the function xtan x -1 defined over all x. Sketch the function to get an idea of the roots 1 find the first couple of roots using bisection to a precision of machine epsilon 2 after straddling a root, find its value using the Newton-Raphson method. 3 after straddling a root, find its value using the secant method 4 after straddling a root, find its value using the false position method. Determine the order of the methods and comment...

  • Only the matlab nlinear equations x 0.75 Determine the roots of these equations using: a) The...

    Only the matlab nlinear equations x 0.75 Determine the roots of these equations using: a) The Fixed-point iteration method. b) The Newton Raphson method. Employ initial guesses of x y 1.2 and perform the iterations until E.<10%. Note: You can use to solve the problems, but you should sol at least two full iterations manually. AB bl Du Thursd 30/3/ 1. For the displacement in Q3 y 10 e cos at 0 St S 4. a) Plot the displacement y...

  • algorithm 2.2 Calculus Suppose you want to find zeros of the function f(x)102212 and plan to...

    algorithm 2.2 Calculus Suppose you want to find zeros of the function f(x)102212 and plan to use the Newton-Raphson scheme. (a) Write down the Newton-Raphson algorithm for this. That is, write down explicitly a formula for computing your (n+1)st guess Tn+1 given your nth guess rn for a root. In other words, deter- mine the recurrence relation resulting from using this particular function f. (b) Modifying Algorithm 2.2 as required, find the values through r7 if you choose an initial...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT