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

P1. Given a function f in C [a, b]. Write a detailed MATLAB function named diffS

ID: 3569441 • Letter: P

Question

P1.

Given a function f in C [a, b]. Write a detailed MATLAB function named diffSigns and a script p1.m that tries to find a subinterval of the interval [a, b] where the function has values with different signs. The length of the subintervals is equal to

h = (b - a) / h and the endpoints of the subintervals are determined by the points x0, x1, x2,..., xn where:

x0 = a

xk = xk+1 +h for k = 1,2,... N

If the function succeed to find a subinterval [xk-1, xk] for which the function has values f(xk-1) and f(xk) with different signs the MATLAB library function fzero() should be called to calculate and prints out an approximation of a root of the equation f(x) = 0

If there is no k(k = 1,2,..., N) for which f(xk-1) and f(x) have different signs the diffSigns function should print an appropriate message.

Input parameters for diffSigns function:

* f

Given a function f in C [a, b]. Write a detailed MATLAB function named diffSigns and a script p1.m that tries to find a subinterval of the interval [a, b] where the function has values with different signs. The length of the subintervals is equal to h = (b - a) / h and the endpoints of the subintervals are determined by the points x0, x1, x2,..., xn where: x0 = a xk = xk+1 +h for k = 1,2,... N If the function succeed to find a subinterval [xk-1, xk] for which the function has values f(xk-1) and f(xk) with different signs the MATLAB library function fzero() should be called to calculate and prints out an approximation of a root of the equation f(x) = 0 If there is no k(k = 1,2,..., N) for which f(xk-1) and f(x) have different signs the diffSigns function should print an appropriate message. Input parameters for diffSigns function: * f ? function defined in the interval [a,b] * a,b ? endpoints of the interval [a,b] * N ? the number of subintervals of [a,b] Write a script that calls difSigns function. Execute the script three times with the following input data: f(x) = x^7 + 9x^5 - 13x - 17 a = 0, b = 8 N = 1000 f(x) = x^7 + 9x^5 - 13x - 17 a = 2, b = 0 N = 50 f(x) = x*exp(-x) - cos(x) a = -5, b = 5 N =50

Explanation / Answer

#include<stdio.h>

#define SIZE 50

struct student {

  char name[30];

  int rollno;

  int sub[3];

};

void main() {

  int i, j, max, count, total, n, a[SIZE], ni;

  struct student st[SIZE];

  clrscr();

  printf("Enter how many students: ");

  scanf("%d", &n);

  /* for loop to read the names and roll numbers*/

  for (i = 0; i < n; i++) {

     printf(" Enter name and roll number for student %d : ", i);

     scanf("%s", &st[i].name);

     scanf("%d", &st[i].rollno);

  }

  /* for loop to read ith student's jth subject*/

  for (i = 0; i < n; i++) {

     for (j = 0; j <= 2; j++) {

       printf(" Enter marks of student %d for subject %d : ", i, j);

       scanf("%d", &st[i].sub[j]);

     }

  }

  /* (i) for loop to calculate total marks obtained by each student*/

  for (i = 0; i < n; i++) {

     total = 0;

     for (j = 0; j < 3; j++) {

       total = total + st[i].sub[j];

     }

     printf(" Total marks obtained by student %s are %dn", st[i].name,total);

     a[i] = total;

  }

  /* (ii) for loop to list out the student's roll numbers who

   have secured the highest marks in each subject */

  /* roll number who secured the highest marks */

  for (j = 0; j < 3; j++) {

     max = 0;

     for (i = 0; i < n; i++) {

       if (max < st[i].sub[j]) {

           max = st[i].sub[j];

           ni = i;

       }

     }

     printf(" Student %s got maximum marks = %d in Subject : %d",st[ni].name, max, j);

  }

  max = 0;

  for (i = 0; i < n; i++) {

     if (max < a[i]) {

       max = a[i];

       ni = i;

     }

  }

  printf(" %s obtained the total highest marks.", st[ni].name);

  getch();

}