I am writing a half-division method on C.

#include <stdio.h> #include <stdlib.h> #include <math.h> #define PI 3.14159 #define E 1e-5 #define A -5 #define B 2 double formula (double b) { double function_res = 2*b*b-9; //пока считаю по этой функции и все равно неправильно //pow(b, 3)* cos(3*PI*b)*exp(b)-pow(b,2)*cos(2*PI*b)*exp(0.5*b)+5; return function_res; } void halfDiv () { double* a = (double*)malloc(sizeof(double)); a[0] = A; double* b = (double*)malloc(sizeof(double)); b[0] = B; double* c = (double*)malloc(sizeof(double)); c[0] = (a[0] + b[0])/2; double* distance = (double*)malloc(sizeof(double)); distance[0] = abs(b[0]-a[0]); double* fa = (double*)malloc(sizeof(double)); fa[0] = formula(a[0]); double* fb = (double*)malloc(sizeof(double)); fb[0] = formula(b[0]); double* fc = (double*)malloc(sizeof(double)); fc[0] = formula(c[0]); int i = 0; printf("a\t\tb\t\tc\t\t|ba|\t\tf(a)\t\tf(b)\t\tf(c)\n%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", a[0], b[0], c[0], distance[0], fa[0], fb[0], fc[0]); while (abs(distance[i] - E)*1e+5 > 10) { i++; a = (double*)realloc(a, (i +1)*sizeof(double)); b = (double*)realloc(b, (i +1)*sizeof(double)); c = (double*)realloc(c, (i +1)*sizeof(double)); distance = (double*)realloc(distance, (i +1)*sizeof(double)); fa = (double*)realloc(fa, (i +1)*sizeof(double)); fb = (double*)realloc(fb, (i +1)*sizeof(double)); fc = (double*)realloc(fc, (i +1)*sizeof(double)); a[i] = (fc[i - 1]<0) ? c[i - 1] : a[i - 1]; b[i] = (fc[i-1]>0) ? c[i-1] : b[i-1]; c[i] = (a[i]+b[i])/2; distance[i] = abs(b[i]-a[i]); fa[i] = formula(a[i]); fb[i] = formula(b[i]); fc[i] = formula(c[i]); printf("%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", a[i], b[i], c[i], distance[i], fa[i], fb[i], fc[i]); } double x = c[sizeof(c)/sizeof(c[0])-1]; double fx = formula(x); printf("\n\nx = %lf\nf(x) = %lf"); return; } int main() { halfDiv(); return 0; } 

here is the code. but for some reason he thinks everything is wrong. Neither the function is wrong, nor even the distance, which the program calculates in general it is not clear which side All seems to be in the same data type. Tell me, please, what's the problem.

  • By the method of half division, you can solve a number of problems, and what is the problem? - nick_n_a
  • At least you don’t have the whole code, it’s hard to evaluate ни даже distance - what are A and B equal to ? we don’t know - iksuy
  • Why do you need an array? a and a2 b and b2 ... And one more .... Zest method: the delta is less than the epsilon . This is your fault. Those. abs(значение_текущее - значение предыдущее) < Epsilon current_value abs(значение_текущее - значение предыдущее) < Epsilon if you have 10 - epsilon, then what is E? Roughly speaking, distance[i] - distance[i-1] should be (instead of distance-E), but under initial conditions it can be a snag, so they use a do-while loop rather than a while-do. - nick_n_a
  • Yeah, your distance is the delta. Well ... then the expression distance[i]=abs(b[i]-a[i]); I do not like. Type in Google method, look through, for example, here is cyberforum.ru/cpp-beginners/thread246601.html a good implementation. - nick_n_a
  • ok, thanks, take a look - Prometheus Music

0