laboratory 2 false-position

2
Gerfel Philip Gonzales BSECE 5 False-Position Method 1 SCINOTE CODE 2 SIMULATION Find the root of +−= with a approximation error of . and maximum number of iteration of 100. //Gerfel Philip C. Gonzales //BSECE 5 //Laboratory 2 //False Position Method function false_position() f = input("Input the function to be evaluated:","string"); a = input("Input initial lower guess a:"); b = input ("Input initial upper guess b:"); ea = input("Input your desired approximation error:"); maxiter = input("Maximum no. of iterations:"); disp(['Lower Root Upper f(a) f(m) f(b)']) iter = 0; while 1==1; iter = iter + 1; x = a; fa = evstr(f); x = b; fb = evstr(f); m = (b*fa - a*fb)/(fa-fb); x = m; fm = evstr(f); er = abs(fm); printf('%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f \n',a ,m ,b ,fa ,fm ,fb); //evaluation if ((fa<0 & fb<0)|(fa>0 &fb >0)) then break end; if fa*fm == 0 then break end; if (fa*fm > 0) a = m; end if (fa*fm < 0) b = m; end if (er <= ea) then break end; if iter == maxiter then break end; end if ((fa<0 & fb<0)|(fa>0 &fb >0)) then printf('\n\n No Root!') else printf('\n\nRoot is %f with %f error at %dth iteration',m,er,iter); end endfunction

Upload: gp-gonzales

Post on 11-Nov-2015

214 views

Category:

Documents


2 download

DESCRIPTION

Sci-Lab function for False-position method by Gerfel Philip C. Gonzales, a prestigious student of Harvard University. Just kidding. I'm just a poor boy, I dont need sympathy. because I easy come, easy go, little high, little low, anywhere the wind blows... you know the rest

TRANSCRIPT

  • Gerfel Philip Gonzales BSECE 5

    False-Position Method

    1 SCINOTE CODE

    2 SIMULATION

    Find the root of + = with a approximation error of . and maximum number of

    iteration of 100.

    //Gerfel Philip C. Gonzales

    //BSECE 5

    //Laboratory 2

    //False Position Method

    function false_position()

    f = input("Input the function to be evaluated:","string");

    a = input("Input initial lower guess a:");

    b = input ("Input initial upper guess b:");

    ea = input("Input your desired approximation error:");

    maxiter = input("Maximum no. of iterations:");

    disp(['Lower Root Upper f(a) f(m) f(b)'])

    iter = 0;

    while 1==1;

    iter = iter + 1;

    x = a;

    fa = evstr(f);

    x = b;

    fb = evstr(f);

    m = (b*fa - a*fb)/(fa-fb);

    x = m;

    fm = evstr(f);

    er = abs(fm);

    printf('%8.4f %8.4f %8.4f %8.4f %8.4f %8.4f \n',a ,m ,b ,fa ,fm ,fb);

    //evaluation

    if ((fa0)) then break end;

    if fa*fm == 0 then break end;

    if (fa*fm > 0) a = m; end

    if (fa*fm < 0) b = m; end

    if (er

  • Gerfel Philip Gonzales BSECE 5

    2.1 CHOOSING A GUESS ROOT OF 0 TO 1

    2.2 CHOOSING A GUESS ROOT OF -5 TO 0