Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Solve in Matlab Write a script file that employs any combination of the flow con

ID: 3028457 • Letter: S

Question

Solve in Matlab
Write a script file that employs any combination of the flow control commands to generate the given matrix. B = [1 1 0 0 0 0 0 -2 2 0 0 0 -1 0 3 3 0 0 0 1 0 -4 4 0 0 0 -1 0 5 5 0 0 0 1 0 -6] Write a user-defined function with function call [opt, k] -opt_finder (fp, x0, kmax, to1) where fp is the derivative (as an inline function) of a given function f. x_0 is a specified value, kmax is the maximum number of iterations, and tol is specified tolerance. The function sets x_1 = x_0, calculates |fp(x_1)|, and if it is less then tolerance, then x_1 approximates the critical point opt at which the derivative is near zero. If not, it will increment x_1 by 0.1 to obtain x_2, repeat the procedure, and soon. The process terminates as soon as |fp(x_1)|

Explanation / Answer

3

function y = f(x)

   y = exp(-x^2)+2*sin(x)-0.4;

>> format long

>> eps_abs = 1e-5;

>> eps_step = 1e-5;

>> a = -2.0;

>> b = 4.0;

>> step_size = Inf;

>> while (step_size >= eps_step || ( abs( f(a) ) >= eps_abs && abs( f(b) ) >= eps_abs ) )

    c = (f(a)*b - f(b)*a)/(f(a) - f(b));

    if ( f(c) == 0 )

       break;

    elseif ( f(a)*f(c) < 0 )

       step_size = b - c;

       b = c;

    else

       step_size = c - a;

       a = c;

    end

end

>> [a b]

ans = 1.259915864579067   2

>> abs(f(a))

ans =    0.0000246934256663

>> abs(f(b))

ans =    6

function [ r ] = false_position( f, a, b, N, eps_step, eps_abs )

    % Check that that neither end-point is a root

    % and if f(a) and f(b) have the same sign, throw an exception.

    if ( f(a) == 0 )

                  r = a;

                  return;

    elseif ( f(b) == 0 )

                  r = b;

                  return;

    elseif ( f(a) * f(b) > 0 )

        error( 'f(a) and f(b) do not have opposite signs' );

    end

    % We will iterate N times and if a root was not

    % found after N iterations, an exception will be thrown.

    c_old = Inf;

    for k = 1:N

        % Find the false position

        c = (a*f(b) + b*f(a))/(f(b) - f(a));

        % Check if we found a root or whether or not

        % we should continue with:

        %          [a, c] if f(a) and f(c) have opposite signs, or

        %          [c, b] if f(c) and f(b) have opposite signs.

        if ( f(c) == 0 )

            r = c;

            return;

        elseif ( f(c)*f(a) < 0 )

            b = c;

        else

            a = c;

        end

        % If |b - a| < eps_step, check whether or not

        %       |f(a)| < |f(b)| and |f(a)| < eps_abs and return 'a', or

        %       |f(b)| < eps_abs and return 'b'.

        if ( abs( c - c_old ) < eps_step )

            if ( abs( f(a) ) < abs( f(b) ) && abs( f(a) ) < eps_abs )

                r = a;

                return;

            elseif ( abs( f(b) ) < eps_abs )

                r = b;

                return;

            end

        end

                  c_old = c;

    end

    error( 'the method did not converge' );

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote