0 to 1 but it doesn't. could anybody help me to solve this problem?
Could it be because you change its value after writing to the file?
I don't think so, because it must be 1 at the end of the lope.
Is this how you write output?? #include <iostream> #include <fstream> using namespace std; int main() { ofstream myfile("RK-exam.txt"); int n; double a=0, b=1, h=0.1; n = (b - a) / h; double x=a,XX, y=1,YY, k,KK; double prime; for (int i = 0; i < n; i++) { prime = ((-1)*x*y) + ((4*x)/y); KK = h * prime; x = x + (h/3); YY = y + (KK/3); prime = ((-1)*x*YY) + ((4*x)/YY); k = h * prime; XX = x + ((2*h)/3); y = YY + ((2*k)/3); prime = ((-1)*XX*y) + ((4*XX)/y); k = h * prime; x = XX; y = YY + (1/4) * (KK + (3*k)); cout << x << "\t" << y << endl; myfile << x << "\t" << y << endl; } myfile.close(); return 0; } 🙃
See the magic!! Change value of h=0.1 to 0.01🙃
yes,,it's solved, thx,🌷 but the point is that h should be 0.1😅
Now probs! Let people play with “h”😜
I mean the const numbers should not be changed, I think there is s.th wrong in the for loop🤔
excuse me, I think you didn't change the value of h🤔, in here we don't have x1, x2,x3. would you explain why you didn't use it?
In short!! I squeezed your logic😄
I know😅, actually this why I have struggle with programming, take a look at this code, if you don't mind #include <iostream> #include <fstream> #include <cmath> using namespace std; int main() { ofstream myfile("RK4.txt"); double k1, k2, k3, k4, x0, x1, x2, x3, x4, y0,y1, y2, y3, y4, y_1_prime, y_2_prime, y_3_prime, y_4_prime, a, b, h, y_new; int n; const double e = 2.71828; a = 0; b = 1; h = 0.1; n = (b - a) / h; x0 = a; y0 = 1; cout<<"y0"<<y0<<endl; for (int i = 0; i <= n; i++) { x1 = x0; cout<<"x1="<<x1<<endl; y1= y0; y_1_prime = ((5 * pow(x1, 2)) - y1) / pow(e, (x1 + y1)); k1 = h * y_1_prime; x2 = x1 + (h / 2);cout<<"x2="<<x2<<endl; y2 = y1 + (k1 / 2); y_2_prime = ((5 * pow(x2, 2)) - y2) / pow(e, (x2 + y2)); k2 = h * y_2_prime; x3 = x1 + (h / 2);cout<<"x3="<<x3<<endl; y3 = y1 + (k2 / 2); y_3_prime = ((5 * pow(x3, 2)) - y3) / pow(e, (x3 + y3)); k3 = h * y_3_prime; x4 = x1 + h;cout<<"x2="<<x4<<endl; y4 = y1 + k3; y_4_prime = ((5 * pow(x4, 2)) - y4) / pow(e, (x4 + y4)); k4 = h * y_4_prime; y_new = y1 + (k1 + 2 * k2 + 2 * k3 + k4) / 6; cout << x0 << "\t" << y_new << endl; //cout<<"k1="<<k1<<" k2="<<k2<<" k3="<<k3<<" k4="<<k4<<endl; myfile << x0 << "\t" << y_new << endl; y0 = y_new; x0 = x4; } myfile.close(); return 0; }
in here I used x1, x2,x3,...and it works accurately
Just modify little and counter will move from 0 to 1, as of now it’s moving it from 0.1 to 1; Here.. — ... for (int i=0; i<n; i++) { cout << x << "\t" << y << endl; myfile << x << "\t" << y << endl; prime = ((-1)*x*y) + ((4*x)/y); KK = h * prime; x = x + (h/3); YY = y + (KK/3); prime = ((-1)*x*YY) + ((4*x)/YY); k = h * prime; XX = x + ((2*h)/3); y = YY + ((2*k)/3); prime = ((-1)*XX*y) + ((4*XX)/y); k = h * prime; x = XX; y = YY + (1/4) * (KK + (3*k)); } cout << x << "\t" << y << endl; myfile << x << "\t" << y << endl; ...
but in the previous code it didn't and just God knows why I think😁
It’s still the same!! Just the same variables are getting overwritten.
I didn't understand🤔
Instead of using x1,2,3 y1,2,3 prime1,2,3 .. I just deduced the code logic to limited use. That make code more reasonable at assembly level and gets optimized well; else you are creating unnecessary variables which are not good for optimization. Smart codes gets optimized at assemble smartly by compiler for better executing! This is just a small code! Habits make big code smartly driven and well optimized. 🤪😄
you're right👌👌, I forgot that factor😅 would you give me some tips improve this skill except practicing? Do you know any special course?😁 how can I learn coding smartly?
Code regularly!! It’s more like a fitness program😄
😂👌, it's the same as practicing😁, thx for your time🙏
Learning is the key... Thought to change it with lambda.. — include <iostream> #include <fstream> #include <cmath> using namespace std; int main() { ofstream myfile("prime-test.txt"); int n; double a=0, b=1, h=0.1; n = (b-a)/h; double x=a, _x; double y=1, _y; double k, _k; cout.setf(ios::fixed); cout.precision(7); cout << x << ":—\t" << y << endl; myfile << x << ":—\t" << y << endl; auto primeX = [] (double x, double y) { const double c = 2.71828; double prime = ((5*pow(x,2))-y)/pow(c,(x+y)); double k = (0.1*prime); return (k); }; for(int i=0; i<n; i++) { _x = x; _y = y; k = primeX(_x, _y); //1 _k = (k+2); _x = x + (h/2); _y = y + (k/2); k = primeX(_x, _y); //2 _k = _k*(k+2); _x = x + (h/2); _y = y + (k/2); k = primeX(_x, _y); //3 _k = _k*(k); _x = x + h; _y = y + k; k = primeX(_x, _y); //4 _k = _k+(k); x = _x; y = y + (_k)/6; cout << x << ":—\t" << y << endl; myfile << x << ":—\t" << y << endl; } myfile.close(); return 0; } 🙃
when a c programmer writes c++, :P
Обсуждают сегодня