Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <vector>
- #include <math.h>
- #include <time.h>
- #include <stdlib.h>
- using namespace std;
- double c = 0.01;
- double eps = 0.00000001;
- double calcFirstX (vector<double> vec) {
- return 4.0*vec[0] - 2.0* vec[1] - 2.0;
- }
- double calcFirstY (vector<double> vec) {
- return 4.0*vec[1] - 2.0* vec[0] - 2.0*vec[2];
- }
- double calcFirstZ (vector<double> vec) {
- return 2.0* vec[2] - 2.0*vec[1];
- }
- double calcSecondX (vector<double> vec) {
- return 12 * pow(vec[0], 3.0) + 12 * pow(vec[0], 2.0) - 24 * vec[0];
- }
- double calcSecondY (vector<double> vec) {
- return 24 * vec[1] - 24;
- }
- void gradientFirst (vector<double> input) {
- vector<double> vectorNew;
- bool znalazlem = true;
- double max = 0.0;
- //Zerowanie
- for (int i=0; i<3; i++) {
- vectorNew.push_back(0.0);
- }
- while (znalazlem) {
- //Odejmowanie od pochodnej
- vectorNew[0] = input[0] - c * calcFirstX(input);
- vectorNew[1] = input[1] - c * calcFirstY(input);
- vectorNew[2] = input[2] - c * calcFirstZ(input);
- max = 0.0;
- for (int i=0; i<3; i++) {
- if (fabs(vectorNew[i] - input[i]) > max) {
- max = fabs(vectorNew[i] - input[i]);
- }
- }
- //Jezeli max mniejszy od bledu
- if (max < eps) {
- printf("Punkt: %f, %f, %f\nWartosc w punkcie: %f", vectorNew[0], vectorNew[1], vectorNew[2], 2 * pow(vectorNew[0], 2.0)
- + 2 * pow(vectorNew[1], 2.0)
- + vectorNew[2]*vectorNew[2]
- - 2 * vectorNew[0] * vectorNew[1]
- - 2 * vectorNew[2] *vectorNew[1]
- - 2* vectorNew[0]
- + 3);
- znalazlem = false;
- }
- input = vectorNew;
- }
- }
- void gradientSecond (vector<double> input) {
- vector<double> vectorNew;
- bool znalazlem = true;
- double max = 0.0;
- for (int i=0; i<2; i++) {
- vectorNew.push_back(0.0);
- }
- while (znalazlem) {
- vectorNew[0] = input[0] - c * calcSecondX(input);
- vectorNew[1] = input[1] - c * calcSecondY(input);
- max = 0.0;
- for (int i=0; i<2; i++) {
- if (fabs(vectorNew[i] - input[i]) > max) {
- max = fabs(vectorNew[i] - input[i]);
- }
- }
- if (max < eps) {
- printf("Punkt: %lf, %lf\nWartosc w punkcie: %lf", vectorNew[0], vectorNew[1], vectorNew[0]*vectorNew[0]*vectorNew[0]*vectorNew[0] * 3
- + 4* vectorNew[0]*vectorNew[0]*vectorNew[0]
- - 12 * vectorNew[0]*vectorNew[0]
- + 12*vectorNew[1] * vectorNew[1]
- - 24 * vectorNew[1]);
- znalazlem = false;
- }
- input = vectorNew;
- }
- }
- int main(int argc, char** argv) {
- srand(time(NULL));
- //Pierwsza funkcja
- /////////////////////////////////////
- printf("\n\n\n");
- cout << "Przyklad a)" << endl;
- vector<double> vectorFirst;
- vectorFirst.push_back((double)rand());
- vectorFirst.push_back((double)rand());
- vectorFirst.push_back((double)rand());
- gradientFirst(vectorFirst);
- printf("\n\n\n");
- //Druga funkcja
- ////////////////////////////////////
- cout << "Przyklad b) " << endl;
- vector<double> vectorSecond;
- vectorSecond.push_back(4.0);
- vectorSecond.push_back(4.0);
- gradientSecond(vectorSecond);
- printf("\n\n\n");
- printf("\n\n\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment