Question

Hi, we recently had an assignment and I ended up skipping this question because I didn't understand the question nor how to even start it. Obviously for Matlab! Coding is not my strong point so this was a stitch up. The data we were meant to use is below!

2(a) Let d be the last digit of your student ID number. But if d0, take d Example l . 1 of Chapter 4, suppose we want u ( 1 2(b) Repeat part (a), but this time use the Newton-Raphson method in the function newton in newton.m. The input parameters are

For (a)

function [n,alpha]=bisect(a,b,eps)
   alpha=(a+b)/2
   n=1;
   fval=f(alpha);
   while (b-alpha> eps) & (fval ~= 0)
      fa=f(a);
      if fa*fval< 0
         b=alpha;
      else
         a=alpha;
      end
      alpha=(a+b)/2
      n=n+1;
      fval=f(alpha);
   end
end

Sample f.m

function y=f(w)
   y=cos(w)-w;
end

For (b)

function [n,alpha]=newton(x0,eps,maxits)
   alpha=x0;
   [fval,deriv]=func(alpha);
   n=0;
   while (abs(fval) > eps) & (n <= maxits)
      alpha=alpha-fval/deriv
      n=n+1;
      [fval,deriv]=func(alpha);
   end
end

func.m

function [f,fdash]=func(w)
   f=cos(w)-w;
   fdash=-sin(w)-1;
end
2(a) Let d be the last digit of your student ID number. But if d0, take d Example l . 1 of Chapter 4, suppose we want u ( 1 2) = d, that is, 6. Then in g(e240 - 1) d. o(e240 + 1) With g -9.8, use the bisection method as implemented in the Matlab function bisect in bisect.m with error tolerance eps 10-8 to find an approximation to o. Please hand in an explanation of how your a and b were found as well as the last ten approximations from the bisection method if it took at least 10 iterations or all the approximations if it took fewer than ten. You should set format long before invoking the function by typing bisect (a,b,eps). A sample f.m file containing the function f required by bisect is also available from Moodle. The function f is currently set to cos(o) - in fm. Note that the function in the bisect.m file on Moodle is slightly different from the one given in Section 8 of the Matlab notes in that the version on Moodle will output to the screen the approximations as they are calculated and also gives the number of iterations at the end in the variable ans.
(b) Repeat part (a), but this time use the Newton-Raphson method in the function newton in newton.m. The input parameters are the initial value x0, error tolerance eps, and maximum number of iterations maxits. You should take eps-108 and maxits 10. The function may be invoked from Matlab by typing newton (x0, eps, maxits) The output to the screen are the approximations as they are calculated and the variable ans at the end is the number of iterations required. A sample func.m file containing the function func required by newton is also available from Moodle. The function func is where f(a) and f,(a) are defined and currently is for f(a) = cos(a)-a. In Matlab, the command for e* is exp (x). Please hand in your expression for f'(), an explanation of how you chose x0, as well as all the approximations. (c) From your numerical results, which of the two methods above seemed to be the better method?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have assumed d=6 but u can change it to yours just by changing it int f.m and func.m

Since the codes were given you were only supposed to find a and b values. '

we can see if we put w<-1 then the fraction is approximately -9.8

which would be < d irrespective of d.

similarly id w>1 then the fraction is>9.8 and faction>d irrespective of d.

You only need to change f and func corresponding to this function and call bisection and newton raphson code.

We can see newton Raphson takes much less iterations to complete

here are the codes:-

Bisect

function [n,alpha]=bisect(a,b,eps)

alpha=(a+b)/2;

n=1;

fval=f(alpha);

while (b-alpha> eps) & (fval ~= 0)

fa=f(a);

if fa*fval< 0

b=alpha;

else

a=alpha;

end

alpha=(a+b)/2;

n=n+1;

fval=f(alpha);

end

end

newton

function [n,alpha]=newton(x0,eps,maxits)

alpha=x0;

[fval,deriv]=func(alpha);

n=0;

while (abs(fval) > eps) & (n <= maxits)

alpha=alpha-fval/deriv;

n=n+1;

[fval,deriv]=func(alpha);

end

end

f

function y=f(w)

%i am assuming d=6 you can change it in the line below

d=6;

y=9.8*(exp(24*w)-1)/(w*(exp(24*w)+1))-d;

end

func

function [f,fdash]=func(w)

d=6;

f=9.8*(exp(24*w)-1)/(w*(exp(24*w)+1))-d;

fdash=9.8*(24*(exp(24*w)*(w*(exp(24*w)+1)))-(exp(24*w)-1)*((exp(24*w)+1)+w*(exp(24*w)*24)))/((w*(exp(24*w)+1))^2);

end

>> format long [n,ans]-bisect(-2,1,10 (-8)) n- 29 ans 1.633333338424563

[n, ans]-newton(- .5,104-8,500e) >> n- fi ans 1.633333333220014

function [n,alpha]-newton(xe, eps,maxits) alpha-xe; [fval, deriv-func (alpha); n=0 ; while (abs(fval) > eps) & (n <= alpha-alFunction [f,fdash1-func (w) d-6; f-9.8* (exp(24%)-1)/(w* ( exp(24%) +1))-d; fdash-9.8*( 24(exp(24w) (w(exp(24w)+1)))-(ex1 function y-f(w) %i am assuming d-6 you can change it in the line below 3-d-6; y-9.8* (exp(24%)-1)/(w*(exp(24%)+1))-d; 4- 5efunction [n,alpha]-bisect (a,b,eps) alpha-(a+b)/2; 1 n=1; fval-f (alpha); while (b-alpha» fa-f(a); & (fva1 5-[ eps) e) ~= if

Add a comment
Know the answer?
Add Answer to:
Hi, we recently had an assignment and I ended up skipping this question because I didn't understand the question nor...
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
  • last digit is 5 bisect f.m newton.m func.m 2(a) Let d be the last digit of your student ID number. But if d0, t...

    last digit is 5 bisect f.m newton.m func.m 2(a) Let d be the last digit of your student ID number. But if d0, take d6. Then in Example 1.1 of Chapter 4, suppose we want v(12)d, that is, g(e24-I) With g-9.8, use the bisection method as implemented in the Matlab function bisect in bisect.m with error tolerance eps 108 to find an approximation to . Please hand in an explanation of how your a and b were found as well...

  • This is Matlab Problem and I'll attach problem1 and its answer for reference. We were unable...

    This is Matlab Problem and I'll attach problem1 and its answer for reference. We were unable to transcribe this imageNewton's Method We have already seen the bisection method, which is an iterative root-finding method. The Newton Rhapson method (Newton's method) is another iterative root-finding method. The method is geometrically motivated and uses the derivative to find roots. It has the advantage that it is very fast (generally faster than bisection) and works on problems with double (repeated) roots, where the...

  • Page 73, as mentioned in the stated question, is provided below 1. Consider a new root-finding...

    Page 73, as mentioned in the stated question, is provided below 1. Consider a new root-finding method for solving f(x) = 0. Successive guesses for the root are generated from the following recurrence formula: Xn+1 = In f(xn) fhen + f(xn)] – F(Xn) (1) Write a user-defined function with the function call: [r, n] = Root fun (f, xl, tol, N) The inputs and outputs are the same as for the user-defined function Newton described on page 73 of Methods....

  • 5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f,...

    5.1.2 Open Methods - Newton-Raphson Method Xi+1= xi – FOTO Matlab Code Example:4 function mynewtraph (f, f1,x0,n) Xx0; for ilin x = x - f(x)/f1(x); disp (li if f(x) <0.01 f(x))) break end end end Matlab Code from Chapra function [root, ea, iter)=newtraph (func,dfunc, xr, es,maxit,varargin) newtraph: Newton-Raphson root location zeroes 8 [root, ea, iter)-newtraph (func, dfunc, xr, es,maxit,pl,p2, ...): $uses Newton-Raphson method to find the root of fune input: func- name of function 8dfunc = name of derivative of...

  • ____________ % This function is a modified versio of the newtmult function obtained % from % “Ap...

    ____________ % This function is a modified versio of the newtmult function obtained % from % “Applied Numerical Methods with MATLAB, Chapra, % 3rd edition, 2012, McGraw-Hill.” function [x,f,ea,iter]=newtmult(func,x0,es,maxit,varargin) % newtmult: Newton-Raphson root zeroes nonlinear systems % [x,f,ea,iter]=newtmult(f,J,x0,es,maxit,p1,p2,...): % uses the Newton-Raphson method to find the roots of % a system of nonlinear equations % input: % f = the passed function % J = the passed jacobian % x0 = initial guess % es = desired percent relative error...

  • Newton's Method in MATLAB During this module, we are going to use Newton's method to compute...

    Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x° + 3x² – 2x – 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its...

  • in matlab sthat this n is hardly different from the scalar version in Section 4.3 . The root estimates are stored as columns in an array . The Newton step is calculated using a backslash. The fu...

    in matlab sthat this n is hardly different from the scalar version in Section 4.3 . The root estimates are stored as columns in an array . The Newton step is calculated using a backslash. The function norm is used for the magnitude of a vector, instead magnitude of a scalar. Function 4.5,1 (netonsys) Newron's method for a system of equations function x=newtonsys (f,x1) 2 % NETONSYS Newton's method for a system of equations 3 % Input : function that...

  • I'm working on the newton's method on matlab, could someone help me and show what two...

    I'm working on the newton's method on matlab, could someone help me and show what two lines are needed to be added in the codes in order to make this function work? function sample_newton(f, xc, tol, max_iter) % sample_newton(f, xc, tol, max_iter) % finds a root of the given function f over the interval [a, b] using Newton-Raphson method % IN: % f - the target function to find a root of % function handle with two outputs, f(x), f'(x)...

  • clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...

    clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold = 0.5; % this is the initial guess test = 1; % this simply ensures we have a test value to enter the loop below. %If we don't preallocate this, MATLAB will error when it trys to start the %while loop below k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it %to allow the while loop to...

  • MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts...

    MATLAB help! I have some MATLAB Assignment to do, the proffesor requires all the small parts in order to get full credit. Help me out, thank you f LAB SECTION NAME HW6P3 (30 points) following are infinite series representations of the function, un pra i a script file HW6P3 that determines the value of the function from the sum of the series for a given value of x. The program (1 pt) must prompt the user to enter a value...

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