Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- double f(double x, double y)
- {
- return (2 * x + y);
- }
- void euler(double a, double b, double h, double y0)
- {
- double x = a;
- while (x < b) {
- y0 = y0 + h * f(x, y0);
- x = x + h;
- cout<<"x="<<x<<"\ty="<<y0<<endl;
- }
- }
- void Rungego(double a, double b, double h, double x0, double y0)
- {
- int n = b / h;
- double k[n];
- x0 = a;
- cout<<"x="<<x0<<"\ty="<<y0<<endl;
- for(int j=0; j < (b / h); j++) {
- k[0] = h * f(x0, y0);
- double suma = k[0];
- //cout<<"k[0]="<<k[0]<<endl;
- for(int i=1; i < (b / h); i++) {
- if(i == (b / h) - 1){
- k[i] = h * f(x0 + h, y0 + k[i-1]);
- suma = suma + k[i];
- }
- else{
- k[i] = h * f(x0 + h / 2, y0 + k[i-1] / 2);
- suma = suma + 2 * k[i];
- }
- // cout<<"k["<<i<<"]="<<k[i]<<endl;
- }
- //cout<<"suma="<<suma<<endl;
- //cout<<"y0="<<y0<<endl;
- y0 += (1 / 6) * suma;
- x0 += h;
- cout<<"x="<<x0<<"\ty="<<y0<<endl;
- }
- }
- int main()
- {
- double a, b, h, x0, y0;
- int opcja;
- cout<<"1. Metoda Eulera\n2. Metoda Rungego - Kutty\n3. Wyjscie\nWybierz opcje: ";
- cin>>opcja;
- do{
- switch(opcja){
- case 1:
- cout<<"Podaj granice [a, b]\na: ";
- cin>>a;
- cout<<"b: ";
- cin>>b;
- cout<<"Podaj krok h: ";
- cin>>h;
- cout<<"Podaj y0: ";
- cin>>y0;
- euler(a, b, h, y0);
- cout<<"1. Metoda Eulera\n2. Metoda Rungego - Kutty\n3. Wyjscie\nWybierz opcje: ";
- cin>>opcja;
- break;
- case 2:
- /*cout<<"Podaj granice [a, b]\na: ";
- cin>>a;
- cout<<"b: ";
- cin>>b;
- cout<<"Podaj krok h: ";
- cin>>h;
- cout<<"Podaj x0: ";
- cin>>x0;
- cout<<"Podaj y0: ";
- cin>>y0;*/
- Rungego(0, 0.4, 0.1, 0, 1);
- cout<<"1. Metoda Eulera\n2. Metoda Rungego - Kutty\n3. Wyjscie\nWybierz opcje: ";
- cin>>opcja;
- break;
- case 3:
- cout<<"Koncze program"<<endl;
- return 0;
- break;
- default:
- cout<<"Bledny wybor. Sprobuj jeszcze raz."<<endl;
- cout<<"1. Metoda Eulera\n2. Metoda Rungego - Kutty\n3. Wyjscie\nWybierz opcje: ";
- cin>>opcja;
- }
- }
- while(true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement