EE518 Homework #8 Zhe Zheng !Q2 function [dx,dy,n,x]=BisectionMethod(a,b,tol_consec,tol_approx) n=0; y1=FunDiff(a); y2=FunDiff(b); x=(a+b)/2; mid=FunDiff(x); p=y1*y2; dx=abs(b-a); dy=abs(mid); if p==0; if y1==0; x=a; elseif y2==0; x=b; end elseif p>0; disp('Change Interval') return; elseif p<0; while dx>tol_consec||dy>tol_approx if mid==0; return; end if sign(mid)==sign(y1); a=x; end if sign(mid)==sign(y2); b=x; end x=(a+b)/2; mid=FunDiff(x); dx=abs(b-a); dy=abs(mid); y1=FunDiff(a); y2=FunDiff(b); n=n+1; end end endfunction y=FunDiff(m) y=exp(-m^2)-m; end !>> [dx,dy,n,x]=BisectionMethod(-2,2,10^-6,10^-6) !dx = ! 9.5367e-07 !!dy = ! 5.5883e-07 !!n = ! 22 !!x = ! 0.6529 !Answer: The point of intersection of f(x) and g(x) on the interval [-2, 2] is x=0.6529. !Q3 function [dx,dy,n,y]=NewtonMethod(x,tol_consec,tol_approx) n=0; j=x; if abs(j)>2 disp('Out of Interval') return end v=FunDiff(j); if v==0 y=j; return else k=DeFunDiff(j); if abs(k)>2 disp('Out of Interval')return end dx=abs(k-j); dy=abs(FunDiff(k)); while dx>tol_consec||dy>tol_approx u=FunDiff(k); if u==0; y=k; return end j=k; k=DeFunDiff(j); if abs(k)>2 disp('Out of Interval') return end dx=abs(k-j); dy=abs(FunDiff(k)); n=n+1; end y=k; end end function h=FunDiff(m) h=exp(-m^2)-m; end function dh=DeFunDiff(m) dh=m-FunDiff(m)/(-2*exp(-m^2)-1); end !>> [dx,dy,n,y]=NewtonMethod(0,10^-6,10^-6) !dx = ! 3.2659e-07 !!dy = ! 1.4802e-07 !!n =! 10 !!y = ! 0.6529 !Answer: The point of intersection of f(x) and g(x) on the interval [-2, 2] is x=0.6529. !Q4 2 package bisection; !public class Bis_zz { ! static double result = 0; static int n = 0; static int a = 10; static int b = -6; static double tol_consec = Math.pow(a, b); static double tol_approx = Math.pow(a, b); static void Bisection(double a, double b){ double mid =(a+b)/2; double y = Math.pow(Math.E, -(Math.pow(mid, 2))) -mid; double ya = Math.pow(Math.E, -(Math.pow(a, 2))) -a; double yb = Math.pow(Math.E, -(Math.pow(b, 2))) -b; if(ya*yb > 0){ System.out.println("change interval"); } else if(ya*yb == 0){ if(ya==0){ result = ya; } else{ result = yb; } } ! else{if ((Math.abs(b-a)<= tol_consec)&&(Math.abs(y)<=tol_approx)) { result = mid; } else { if(y == 0){ result = mid; } else if(y*yb>0){ b =mid ; Bisection(a,b); n++; } else if (y*ya>0){ a = mid; Bisection(a,b); n++; } } } } public static void main(String[] args) { // TODO Auto-generated method stub Bisection(-2,2); System.out.println(result); System.out.println("n="+n); } } !Answer: 0.6529183387756348 !3 package newton; !public class ND_zz { static double result = 0; static int a = 10; static int b = -6; static double tol_consec = Math.pow(a, b); static double tol_approx = Math.pow(a, b); static int n =0;static double Newton(double x){ double c = Math.pow(x, 2); double d = Math.pow(Math.E, -c); double y = d -x; double y1 = (-2)*x*d - 1; double x1 = x - y/y1; if(Math.abs(x)<=2){ if((Math.abs(y)<= tol_approx)&& (Math.abs(x1-x))<=tol_approx){ return result =x; } else{ n++; return Newton(x-y/y1); } } else{ System.out.println("Out of internal"); } return x; } ! public static void main(String[] args) { // TODO Auto-generated method stub Newton(0); System.out.println(result); System.out.println("n="+n); } } !Answer:
View Full Document