Похожие чаты

Https://Pastebin.Com/GsiNuxSK Hi every body, in this code x0 should change from

0 to 1 but it doesn't. could anybody help me to solve this problem?

24 ответов

9 просмотров

Could it be because you change its value after writing to the file?

Paria.-Z Автор вопроса

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; } 🙃

Aakash Saini
Is this how you write output?? #include <iostrea...

See the magic!! Change value of h=0.1 to 0.01🙃

Paria.-Z Автор вопроса
Aakash Saini
Is this how you write output?? #include <iostrea...

yes,,it's solved, thx,🌷 but the point is that h should be 0.1😅

Paria.-Z Автор вопроса
Paria. Z
yes,,it's solved, thx,🌷 but the point is that h sh...

I mean the const numbers should not be changed, I think there is s.th wrong in the for loop🤔

Paria.-Z Автор вопроса
Aakash Saini
Now probs! Let people play with “h”😜

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?

Paria.-Z Автор вопроса
Aakash Saini
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; }

Paria.-Z Автор вопроса
Paria. Z
I know😅, actually this why I have struggle with p...

in here I used x1, x2,x3,...and it works accurately

Paria. Z
excuse me, I think you didn't change the value of ...

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; ...

Paria.-Z Автор вопроса
Paria. Z
in here I used x1, x2,x3,...and it works accuratel...

but in the previous code it didn't and just God knows why I think😁

Paria. Z
in here I used x1, x2,x3,...and it works accuratel...

It’s still the same!! Just the same variables are getting overwritten.

Paria.-Z Автор вопроса
Paria. Z
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. 🤪😄

Paria.-Z Автор вопроса
Aakash Saini
Instead of using x1,2,3 y1,2,3 prime1,2,3 .. I jus...

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?

Paria. Z
you're right👌👌, I forgot that factor😅 would you gi...

Code regularly!! It’s more like a fitness program😄

Paria.-Z Автор вопроса
Aakash Saini
Code regularly!! It’s more like a fitness program😄

😂👌, it's the same as practicing😁, thx for your time🙏

Paria. Z
I know😅, actually this why I have struggle with p...

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; } 🙃

Похожие вопросы

Обсуждают сегодня

Есть какой-нибудь для Delphi/FPC T*Compression(Decompression)Stream на базе LZ4/Zstd/любой другой быстрый(и хорошо сжимающий) алгоритм А ещё лучше в pure pascal А ещё лучше од...
notme
32
А чем вам питонисты не угодили?😂
.
79
Hey guys, did you see the new announcement about $Fun? 🔥
Filip Murphy
55
Язык Си можно выучить за день? По книжке ANSI C на 230 страниц
Vincent Vegan
29
Привет, запускаю werf в dind в k8s, получаю ошибку на этапе build/beforeSetup: /.werf/stapel/embedded/bin/bash: /.werf/scripts/5898bdfe5214357d3706b879cc8d3d78460fb379607cbd...
florius0
7
Всем привет, написал код ниже, но он выдает сегфолт, в чем причина? #include <stdio.h> #include <stdlib.h> #include <string.h> struct product { char *name; float price; };...
buzz базз
75
пацаны, как-нибудь можно панель задач заблюрить, что она была похожа на kde или винду 7, 11?
В
16
or do they all have 240?
Ilnur Zaripov
9
HI. Just reviewd Planck website which looks undercooked and some menu is not leading to correct section of site. Also, the vesting is shown to be for 20 months starting next y...
Neo517
4
Дорогие любители Прекрасной Джулии! Есть кто-то имеющий практический опыт построения ML для Систем Управления? Нам нужно сделать нейросеть для автоматической подстройки пара...
Roman Timo
4
Карта сайта