Question

Looking for Muller Method MatLab coding for these following equations:

f(x) = ?? - 2 f(x) = 23 – 3 f(0) = x+ - 0.06 f(0) = 3 + 3.22 - 1 f(1) = x3 – 22 – 24.0 – 32

This is the code I currently have but is not working

fun = @(x) x.^2-2;
x1 = 0;
x2 = 1;
tol = 0.0001;
kmax = 100;
function [x, y] = Muller(fun, x1, x2, tol, kmax)
x(1) = x1;
x(2) = x2;
x(3) = (x(2)+x(1))/2;
y(1) = feval(fun, x1);
y(2) = feval(fun, x2);
y(3) = feval(fun, x(3));
c(1) = (y(2)-y(1))/(x(2)-x(1));
for k = 3 : kmax
c(k-1) = (y(k)-y(k-1))/(x(k)-x(k-1));
d(k-2) = (c(k-1)-c(k-2))/(x(k)-x(k-2));
s = c(k-1)+(x(k)-x(k-1))*d(k-2);
x(k+1) = x(k)-2*y(k)/(s+sign(s)*sqrt(s^2-4*y(k)*d(k-2)));
y(k+1) = feval(fun, x(k+1));
if abs(x(k+1)-x(k)) < tol
disp('Muller method has converged'); break;
end
iter = k;
end
if iter >= kmax
disp('zero not found to desired tolerance');
end
end

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


clear all close all #1st function f=@(x) x.^2-2; xl=0;x2=1;tol=10^-3; kmax=1000 root=muller(f,x1,x2, tol, kmax); $2nd functioc=f(x2); x31=x2+((-2*c)/(b+sqrt(b*b-4*a*c))); x32=x2+((-2*c)/(b-sqrt(b*b-4*a*c))); checking root closer to zero if abs(f(x31)

clear all
close all

%1st function
f=@(x) x.^2-2;
x1=0;x2=1;tol=10^-3; kmax=1000;
root=muller(f,x1,x2,tol,kmax);

%2nd function
f=@(x) x.^3-3;
x1=0;x2=1;tol=10^-3; kmax=1000;
root=muller(f,x1,x2,tol,kmax);

%3rd function
f=@(x) x.^4-0.06;
x1=0;x2=1;tol=10^-3; kmax=1000;
root=muller(f,x1,x2,tol,kmax);

%4th function
f=@(x) x.^3+3.*x.^2-1;
x1=0;x2=1;tol=10^-3; kmax=1000;
root=muller(f,x1,x2,tol,kmax);

%5th function
f=@(x) x.^3-x.^2-24.*x-32;
x1=0;x2=1;tol=10^-3; kmax=1000;
root=muller(f,x1,x2,tol,kmax);

%function for Muller method
function root=muller(f,x1,x2,tol,kmax)
    %error value for initiation of while loop
    error=10;
    %displaying the function
    fprintf('The function is f(x)=')
    disp(f)   
    %Loop for Muller's method
    x0=x1-0.5; cnt=0;
    while error>=tol
        %function value corresponds to initial guess
        f0=f(x0);
        f1=f(x1);
        f2=f(x2);
        cnt=cnt+1;
        %all other terms for Muller method
        h0=x1-x0;
        h1=x2-x1;

        del0=(f(x1)-f(x0))/h0;
        del1=(f(x2)-f(x1))/h1;

        a=(del1-del0)/(h1+h0);
        b=a*h1+del1;
        c=f(x2);

        x31=x2+((-2*c)/(b+sqrt(b*b-4*a*c)));
        x32=x2+((-2*c)/(b-sqrt(b*b-4*a*c)));

        %checking root closer to zero
        if abs(f(x31))>=abs(f(x32))
            x3=x32;
        else
            x3=x31;
        end
        %percentage error after each iterations
        error=(abs((x3-x2)/x3))*100;
        %changing x values after each iteration
        x0=x1; x1=x2;x2=x3;
        if cnt>=kmax
            break
        end
    end
    root=x3;
    fprintf('\tThe root of the function is %f.\n',x3)
end

   

Add a comment
Know the answer?
Add Answer to:
Looking for Muller Method MatLab coding for these following equations: This is the code I currently...
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
  • % Bisection.m Lines of code 17-26 and 43-47 are bold % This code finds the root...

    % Bisection.m Lines of code 17-26 and 43-47 are bold % This code finds the root of a function f(x) in the interval [a, b] using the Bisection method % % It uses f.m to define f(x), and assumes f(x) is continuous % It requires specification of a, b and the maximum error % It defines error using |f(xnew)| % Define inputs for problem a=0; %Defines lower limit of initial bracketing interval b=1; %Defines upper limit of initial bracketing interval...

  • Please help me fix this halleyroot funtion , in matlab function [x, er, n] = Halleyroot(fun,x1,maxtol,maxitr)...

    Please help me fix this halleyroot funtion , in matlab function [x, er, n] = Halleyroot(fun,x1,maxtol,maxitr) if (nargin < 5), maxitr = 25; end if (nargin < 4), maxtol = 1e-4; end k = 0; er = 1; x = x1; funder= derivative(fun); while er >= maxtol && k < maxitr k = k+1; % increment the iteration number xold = x; x = xold-fun(xold)/funder(xold); er = abs((x-xold)/x); fprintf('iter = %i\t x = %f\t f(x) = %f\t er = %f...

  • Please do this in Matlab. Not sure if you need this code: e cofunction [x, er,...

    Please do this in Matlab. Not sure if you need this code: e cofunction [x, er, n] = FixedPoint(g, x1, maxtol, maxitr) if nargin < 4, maxitr = 25; end if nargin < 3, maxtol = 1e-3; end k = 0 ;    er = 1; x = x1;    while er >= maxtol && k < maxitr k=k+1; xold = x; x=g(x); er=abs((x-xold)/x); fprintf('iter = %i, x = %e, er = %e ', k,x,er); end n=k; if n ==...

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

  • Matlab problem using newton raphson to find square root of number

    Need help modifying my Matlab script below (myscript calculates the square root of a number. using a Newton-Raphson method with 1 as the initial guess, calculates true and estimated error, and shows each iteration).-I need to create three new functions each of which should be called in the main script. These functions are needed to replace code that is currently in my script shown below.-I need to create these functions:A function to find f(x)A function to find f '(x) ?A...

  • 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)...

  • 45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using...

    45-3. Modify the code used in Example 4 to find the root only at f(x)<0.01 using Newton-Rephson Method without showing any iteration. Also find the root of equation, f(x) = x 9-3x -10, take initial guess, Xo=2 العقدة College of 9:05 mybb.qu.edu.ca Numerical Methods (Lab.) GENG 300 Summer 2020 5.1.2 Open Methods - Newton-Raphson Method f(x) *1+1 = x; - Matlab Code Example:4 function mynewtraph.t1.x0,-) XXO for ilin x - x - x)/1 x) disp 1 x) <0.01 break end...

  • How can I make these equations into 6 first order equations to input them in MATLAB...

    How can I make these equations into 6 first order equations to input them in MATLAB as : function yp = ivpsys_fun_oscillator(t, y) % IVPSYS_FUN evaluates the right-hand-side of the ODE's. % Inputs are current time and current values of y. Outputs are values of y'. % Call format: yp = ivpsys_fun_oscillator(t, y) global m k l %% Define ODE for IVP % Reduce high order derivative to first order % y1 -> x1, y2 -> x2, y3 -> x3...

  • Matlab Regula Falsi Method A zero of f(x) = x^2 -4x-12.2 is known to exist on...

    Matlab Regula Falsi Method A zero of f(x) = x^2 -4x-12.2 is known to exist on the interval [1.2 , 2.2 , 3.2,...9.2] and respective right endpoints x1 =[2.2 ,3.2, 4.2....10.2], find the sub-interval in which the zero exists. Set the left endpoint of this sub-interval =a and the right endpoint=b With tolerance of 10^-9, use the Regular Falsi method to compute the root of (f) in [a,b] User input is required at ##### CONTENTS Close Courses LMS Integration Documentation...

  • MATLAB code starts here --------- clear T0=2; w0=2*pi/T0; f0=1/T0; Tmax=4; Nmax=15; %--- i=1; for t=-Tmax: .01:Tmax...

    MATLAB code starts here --------- clear T0=2; w0=2*pi/T0; f0=1/T0; Tmax=4; Nmax=15; %--- i=1; for t=-Tmax: .01:Tmax T(i)=t; if t>=(T0/2) while (t>T0/2) t=t-T0; end elseif t<=-(T0/2) while (t<=-T0/2) t=t+T0; end end if abs(t)<=(T0/4) y(i)=1; else y(i)=0; end i=i+1; end plot(T,y),grid, xlabel('Time (sec)'); title('y(t) square wave'); shg disp('Hit return..'); pause %--- a0=1/2; F(1)=0; %dc freq C(1)=a0; for n=1:Nmax a(n)=(2/(n*pi))*sin((n*pi)/2); b(n)=0; C(n+1)=sqrt(a(n)^2+b(n)^2); F(n+1)=n*f0; end stem(F,abs,(C)), grid, title(['Line Spectrum: Harmonics = ' num2str(Nmax)]); xlabel('Freq(Hz)'), ylabel('Cn'), shg disp('Hit return...'); pause %--- yest=a0*ones(1,length(T)); for n=1:Nmax yest=yest+a(n)*cos(2*n*pi*T/T0)+b(n)*sin(2*n*pi*T/T0);...

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