Monte Carlo yöntemiyle rastgele sayıları kullanarak integral çözmeye uğraşıyorum. Programdan beklenenler şunlar;
-İntegralin kaç kat olduğunun girilmesi (Maksimum 3 kat olacak.) -Fonksiyonun girilmesi (Kullanıcı herhangi bir fonksiyon girebilir. Mesela 3x de olabilir tan(x)/sin(x) de.) -Her integral için alt ve üst limitlerinin girilmesi (Her integral için 2 değer gerekiyor yani 3 katlı olduğunda 6 değer gerekiyor.) -Tekrarlanacak işlem sayısı (İşlem ne kadar çok yapılırsa başarı sayısı o kadar yüksek.)
Şuana kadar fonksiyonun girilmesi hariç tüm adımları tamamladım. Şöyle ki kullanıcının girdiği fonksiyona göre işlemleri nasıl çevirebileceğimi bilmiyorum. Yani kullanıcı x*x girerse fonksiyon sayıları yerine koyup nasıl bu çarpımı yapabilir? Fonksiyonu string olarak tanımlarsam işlemi yapamaz çünkü hepsini karakter alır. Fonksiyonu double veya integer tanımlarsam da bu sefer karakterleri farkedemiyor. Her türlü fikirlerinizi bekliyorum şimdiden teşekkürler.
// Monte Carlo Method for evaluating integrals
#include <cstdlib> #include <iostream> #include <cmath> using namespace std;
double Random(){ return rand()/(RAND_MAX+1.0); }
int main(){ int degree; double upperx,lowerx,uppery,lowery,upperz,lowerz,n,s=0; string function; cout << "Input your degree(1,2,3): " << endl; cin >> degree;
if(degree==1){ cout << "Input your function: " << endl; cin >> function; // How to input an equation ?????????????? cout << "Input upper limit of x: " << endl; cin >> upperx; cout << "Input lower limit of x: " << endl; cin >> lowerx; cout << "Input n: " << endl; cin >> n; for(int i=1; i<=n; i++){ double x = lowerx + (upperx-lowerx)*Random(); s+=function; // How to calculate x values in an equation and sum the results ??????????? } double mean = s/n; double integ = (upperx-lowerx)*mean; cout << integ; }
else if (degree==2){ cout << "Input your function: " << endl; cin >> function; // How to input an equation ?????????????? cout << "Input upper limit of x: " << endl; cin >> upperx; cout << "Input lower limit of x: " << endl; cin >> lowerx; cout << "Input upper limit of y: " << endl; cin >> uppery; cout << "Input lower limit of y: " << endl; cin >> lowery; cout << "Input n: " << endl; cin >> n; for(int i=1; i<=n; i++){ double x = lowerx + (upperx-lowerx)*Random(); double y = lowery + (uppery-lowery)*Random(); s+=function; // How to calculate x and y values in an equation and sum the results ??????????? } double mean = s/n; double integ = (upperx-lowerx)*(uppery-lowery)*mean; cout << integ; }
else if (degree==3){ cout << "Input your function: " << endl; cin >> function; // How to input an equation ?????????????? cout << "Input upper limit of x: " << endl; cin >> upperx; cout << "Input lower limit of x: " << endl; cin >> lowerx; cout << "Input upper limit of y: " << endl; cin >> uppery; cout << "Input lower limit of y: " << endl; cin >> lowery; cout << "Input upper limit of z: " << endl; cin >> upperz; cout << "Input lower limit of z: " << endl; cin >> lowerz; cout << "Input n: " << endl; cin >> n; for(int i=1; i<=n; i++){ double x = lowerx + (upperx-lowerx)*Random(); double y = lowery + (uppery-lowery)*Random(); double z = lowerz + (upperz-lowerz)*Random(); s+=function; // How to calculate x, y and z values in an equation and sum the results ??????????? } double mean = s/n; double integ = (upperx-lowerx)*(uppery-lowery)*(upperz-lowerz)*mean; cout << integ; }
else cout << "Please put a number between 1 and 3!" << endl; }
yeni mesaja git
Yeni mesajları sizin için sürekli kontrol ediyoruz, bir mesaj yazılırsa otomatik yükleyeceğiz.Bir Daha Gösterme