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

MATLAB CODE function mybisect( f,xl,xu,n ) c=feval(f, xl); d=feval(f, xu); if c*

ID: 3601415 • Letter: M

Question

MATLAB CODE

function mybisect( f,xl,xu,n )

c=feval(f, xl); d=feval(f, xu);

if c*d>0.0

    error('Function has same sign at both endpoints')

end

for i=1:n

    xr=(xl+xu)/2;

    yr=feval(f,xr);

   e=abs((xl-xu)/xr);

    if c*yr<0.0

       xu=xr;

    else

       xl=xr;

    end

disp([i xl xu   xr    e])

(a). Modify the code to find the root until the error (e) reached 1% using Bisection Method without showing iteration. Also find out the root of equation, f(x)=1+3x-x2, Use initial and upper guesses 1 and 5

(b). Solve the above problem by graphical method (use fplot)

Please write full code (Full Answer )

function mybisect( f,xl,xu,n )

c=feval(f, xl); d=feval(f, xu);

if c*d>0.0

    error('Function has same sign at both endpoints')

end

for i=1:n

    xr=(xl+xu)/2;

    yr=feval(f,xr);

   e=abs((xl-xu)/xr);

    if c*yr<0.0

       xu=xr;

    else

       xl=xr;

    end

disp([i xl xu   xr    e])

end

Explanation / Answer

8 #include <stdlib.h> 9 #include <unistd.h> 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #include <sys/mman.h> 13 #include <fcntl.h> 14 #include <errno.h> 15 #include <string.h> 16 17 void report_error(char *error) 18 { 19 fprintf(stderr, "Error: %s ", error); 20 21 exit(-1); 22 } 23 24 int main(int argc, char *argv[]) 25 { 26 struct stat statbuf; 27 char *fn; 28 int fd; 29 size_t len, i, count; 30 31 char *data; 32 33 if (argc < 2) { 34 if (argc < 1) { 35 report_error("no command line"); 36 fprintf(stderr, "Usage: %s <file> ", argv[0]); 37 } else { 38 report_error("Not enough arguments"); 39 fprintf(stderr, "Usage: %s <file> ", argv[0]); 40 } 41 } 42 43 fn = argv[1]; 44 45 if (stat(fn, &statbuf)) { 46 report_error(strerror(errno)); 47 } 48 49 len = statbuf.st_size; 50 printf("File %s: ", fn); 51 printf(" inode %ld ", statbuf.st_ino); 52 printf(" length %ld ", len); 53 54 if (S_ISREG(statbuf.st_mode)) { 55 fd = open(fn, O_RDONLY); 56 if (fd == -1) { 57 report_error(strerror(errno)); 58 } 59 data = (char *) mmap(NULL, len, 60 PROT_READ, MAP_SHARED, fd, 0); 61 if (data == MAP_FAILED) { 62 report_error(strerror(errno)); 63 } 64 65 count = 0; 66 for (i=0; i<len; i++) { 67 if (data[i] == 'a') { 68 count++; 69 } 70 } 71 72 printf(" a count %ld ", count); 73 74 if (munmap(data, len) == -1) { 75 report_error(strerror(errno)); 76 } 77 close(fd); 78 } 79 80 return 0; 81 }