Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.80 KB | None | 0 0
  1. function Project1(f,a,b,err);
  2. syms x
  3.  
  4. fprintf(("Enter a function as a polynomial in x \n\n")); % Asking the user to enter the function ..
  5. d = input('f(x) = ');  
  6. f = eval(d); % The function which has entered ..
  7. fprintf("\n");
  8.  
  9. fprintf("Enter the beginning of the section in x \n\n"); % The beginning value ..
  10. a = input('a = ');
  11. fprintf("\n");
  12.  
  13. fprintf("Enter the end of the section in x \n\n"); % The end value ..
  14. b = input('b = ');
  15. fprintf("\n");
  16.  
  17. fprintf("Enter the accuracy \n\n"); % The error value
  18. err = input('e = ');
  19. fprintf("\n");
  20.  
  21. x=[a-5:b+5];
  22. yab=[eval(f)];
  23. xab=x;
  24.  
  25.  
  26.  
  27.  
  28. %--------------------------- input values -----------------------------
  29.  
  30. fx = a;
  31. n = 0;
  32.  
  33. while abs(fx) > err
  34.    
  35.     n = n+1;
  36.     x = a;
  37.     fa = eval(f);
  38.    
  39.     fprintf("Compute the midpoint \n\n");
  40.    
  41.     x = (a+b)/2; % Calculating the mid point value ..
  42.  
  43.     fprintf("%s %.2f","The midpoint is ",x);
  44.     fprintf("\n\n");
  45.    
  46.     fx = eval(f);
  47.     fprintf("Test if midpoint is a zero \n\n");
  48.    
  49.     if fx ~= 0 % Checking if the mid point is zero or not ..
  50.        
  51.         % Printing if the zeros didn't found ..
  52.         fprintf("The midpoint is not a zero \n\n");
  53.         fprintf("Looking for the section containing the zero \n\n");
  54.         fprintf("%s %.2f %s %.2f %s","The zero is in the section is [",a ,",",b ,"]");
  55.         fprintf("\n\n");
  56.      
  57.     end
  58.  
  59.     % Updating the value of a and b ..
  60.     if sign(fx) == sign(fa)
  61.         a = x;
  62.     else
  63.         b = x;
  64.     end
  65.    
  66. end
  67.  
  68.  % Printing if the zeros is found ..
  69.  fprintf("The midpoint is a zero \n\n");
  70.  fprintf ("%s %s %s %.2f %s %.2f %s","The zero of ", d ,"in the section [",a ,",",b ,"] is ");
  71.  
  72.  disp(x);
  73.      
  74.  plot(xab,yab);
  75.  grid;
  76.  
  77. %---------------------------------- End coding -------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement