I have a question. Basicly, I want to calculate a formula. I want to use this formula for transform the numbers in the first array to second array. For example;
My first array is; {178, 693, 1346, 2484, 2848, 3470, 3820}
My second array is; {110, 290, 530, 920, 1300, 1890, 2050}
I want a formula like this: y = a + bx + cx^2 + d*x^3
When i give my first array as an input to this function, it should give me second array as and answer. For example;
110 = a + b*178 + c*178^2 + d*178^3
As you can see, I want my computer to calculate a, b, c and d numbers. The computer should give me the answer as shown below;
'Your numbers are: a=325, b=-258, c=-3 d=412'
I know, this is mathematically impossible to calculate exact a,b,c,d numbers for transformation because there is none. So I only wanted aprox. function. What is this mean? If a function can calculate outputs with %90 accuracy, this is enough for me.
Long story short, I wrote a code for this in c++ (visual studio) but the app crashed imediately. Can you please specify my problem and help me?
#include "iostream"
#include "stdio.h"
long x[] = {178, 693, 1346, 2484, 2848, 3470, 3820};
long y[] = {110, 290, 530, 920, 1300, 1890, 2050};
long y_deneme[7];
long a = -9999999;
long b = -9999999;
long c = -9999999;
long d = -9999999;
long a_tutan[2];
long b_tutan[2];
long c_tutan[2];
long d_tutan[2];
long y_tutan[2][7];
long a_secilen = 0;
long b_secilen = 0;
long c_secilen = 0;
long d_secilen = 0;
long y_secilen[7];
int y_0_dizi_sayac = 0;
int y_1_dizi_sayac = 0;
int tutan_deneme_sayac = 0;
int katsayi_index = 0;
int main() {
printf("Katsayilar deneniyor. a = %ld, b = %ld, c = %ld, d = %ld \n", a, b, c, d);
for (a = -9999999; a < 9999999; a ++) {
for (int i = 0; i < 7; i ++) {
y_deneme[i] = a + (b * x[i]) + (c * x[i] * x[i]) + (d * x[i] * x[i] * x[i]);
}
for (int i = 0; i < 7; i ++) {
if (y_deneme[i] <= (y[i] + y[i] / 10) || y_deneme[i] >= (y[i] - y[i] / 10)) {
tutan_deneme_sayac++;
}
}
if (tutan_deneme_sayac == 6) {
a_tutan[katsayi_index] = a;
b_tutan[katsayi_index] = b;
c_tutan[katsayi_index] = c;
d_tutan[katsayi_index] = d;
printf("Tutan degerler var. a = %ld, b = %ld, c = %ld, d = %ld \n", a_tutan[katsayi_index], b_tutan[katsayi_index], c_tutan[katsayi_index], d_tutan[katsayi_index]);
for (int i = 0; i < 7; i++) {
y_tutan[katsayi_index][i] = y_deneme[i];
}
katsayi_index++;
}
tutan_deneme_sayac = 0;
if (katsayi_index > 1) {
for (int i = 0; i < 7; i++) {
if (y[i] - y_tutan[0][i] < y[i] - y_tutan[1][i]) {
y_0_dizi_sayac++;
}
else {
y_1_dizi_sayac++;
}
}
if (y_0_dizi_sayac < y_1_dizi_sayac) {
for (int i = 0; i < 7; i++) {
y_secilen[i] = y_tutan[0][i];
}
a_secilen = a_tutan[0];
b_secilen = b_tutan[0];
c_secilen = c_tutan[0];
d_secilen = d_tutan[0];
printf("Secilen degerler degisti. a = %ld, b = %ld, c = %ld, d = %ld \n", a_secilen, b_secilen, c_secilen, d_secilen);
}
else {
for (int i = 0; i < 7; i++) {
y_secilen[i] = y_tutan[1][i];
}
a_secilen = a_tutan[1];
b_secilen = b_tutan[1];
c_secilen = c_tutan[1];
d_secilen = d_tutan[1];
printf("Secilen degerler degisti. a = %ld, b = %ld, c = %ld, d = %ld \n", a_secilen, b_secilen, c_secilen, d_secilen);
}
}
}
b++;
if (b > 9999999) {
b = -9999999;
c++;
}
if (c > 9999999) {
c = -9999999;
d++;
}
if (d > 9999999) {
printf("Programin sonu geldi. \n");
printf("En yakin a = %ld, b = %ld, c = %ld, d = %ld \n", a_secilen, b_secilen, c_secilen, d_secilen);
printf("En yakin y0 = %ld, y1 = %ld, y2 = %ld, y3 = %ld, y4 = %ld, y5 = %ld, y6 = %ld \n", y_secilen[0], y_secilen[1], y_secilen[2], y_secilen[3], y_secilen[4], y_secilen[5], y_secilen[6]);
printf("Gerçek y0 = %ld, y1 = %ld, y2 = %ld, y3 = %ld, y4 = %ld, y5 = %ld, y6 = %ld", y[0], y[1], y[2], y[3], y[4], y[5], y[6]);
}
}