Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Project1(f,a,b,err);
- syms x
- fprintf(("Enter a function as a polynomial in x \n\n")); % Asking the user to enter the function ..
- d = input('f(x) = ');
- f = eval(d); % The function which has entered ..
- fprintf("\n");
- fprintf("Enter the beginning of the section in x \n\n"); % The beginning value ..
- a = input('a = ');
- fprintf("\n");
- fprintf("Enter the end of the section in x \n\n"); % The end value ..
- b = input('b = ');
- fprintf("\n");
- fprintf("Enter the accuracy \n\n"); % The error value
- err = input('e = ');
- fprintf("\n");
- x=[a-5:b+5];
- yab=[eval(f)];
- xab=x;
- %--------------------------- input values -----------------------------
- fx = a;
- n = 0;
- while abs(fx) > err
- n = n+1;
- x = a;
- fa = eval(f);
- fprintf("Compute the midpoint \n\n");
- x = (a+b)/2; % Calculating the mid point value ..
- fprintf("%s %.2f","The midpoint is ",x);
- fprintf("\n\n");
- fx = eval(f);
- fprintf("Test if midpoint is a zero \n\n");
- if fx ~= 0 % Checking if the mid point is zero or not ..
- % Printing if the zeros didn't found ..
- fprintf("The midpoint is not a zero \n\n");
- fprintf("Looking for the section containing the zero \n\n");
- fprintf("%s %.2f %s %.2f %s","The zero is in the section is [",a ,",",b ,"]");
- fprintf("\n\n");
- end
- % Updating the value of a and b ..
- if sign(fx) == sign(fa)
- a = x;
- else
- b = x;
- end
- end
- % Printing if the zeros is found ..
- fprintf("The midpoint is a zero \n\n");
- fprintf ("%s %s %s %.2f %s %.2f %s","The zero of ", d ,"in the section [",a ,",",b ,"] is ");
- disp(x);
- plot(xab,yab);
- grid;
- %---------------------------------- End coding -------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement