#include <iostream>
#include <math.h>
using namespace std;
double x[12000] = {0}, y[12000] = {0}, z[12000] = {0};
double f(double x, double y, double z)
{
return 6*x;//2*z-y+x-2;
}
double Y(double x)
{
double C1 = 1, C2 = 1;
return pow(x, 3) + C1*x + C2;//C1*exp(x)+C2*x*exp(x)+x;
}
double L(int n)
{
double s = 0;
for (int i = 0; i < n; i++)
{
s += sqrt(pow(x[i]-x[i+1], 2) + pow(y[i]-y[i+1], 2));
}
return s;
}
void d2(double x0, double y0, double z0, double b, int n)
{
double tau = ((b-x0)+.0)/(n+.0);
x[0] = x0; y[0] = y0; z[0] = z0;
double k1, k2, k3, k4;
double m1, m2, m3, m4;
//cout << "\tn = " << n << "\ntau = " << tau << endl;
for (int i = 0; i <= n; i++)
{
x[i] = x0 + i*tau;
//cout << "x_i = " << x0+i*tau << endl;
m1 = z[i];
k1 = f(x[i], y[i], z[i]);
m2 = z[i]+tau*k1/2.;
k2 = f(x[i] + tau/2., y[i] + tau*m1/2., z[i] + tau*k1/2.);
m3 = z[i]+tau*k2/2.;
k3 = f(x[i] + tau/2., y[i] + tau*m2/2., z[i] + tau*k2/2.);
m4 = z[i]+tau*k3;
k4 = f(x[i] + tau, y[i] + tau*m3, z[i] + tau*k3);
y[i+1] = y[i] + tau * (m1 + 2.*m2 + 2.*m3 + m4)/6.;
z[i+1] = z[i] + tau * (k1 + 2.*k2 + 2.*k3 + k4)/6.;
}
}
double f2(double x0, double y0, double beta, double b, int n)
{
d2(x0, y0, beta, b, n);
return L(n);
}
double gs(double x0, double y0, double b, int n)
{
double x11 = -100, x22 = 100, x1, x2;
double md = (1 + sqrt(5)) / 2, eps = 1e-6;
int i = 0;
//std::cout << "i a b |a - b| \n";
while (fabs(x11 - x22) >= eps)
{
x1 = x22 - (x22 - x11) / md;
x2 = x11 + (x22 - x11) / md;
if (f2(x0, y0, x1, b, n) >= f2(x0, y0, x2, b, n)) x11 = x1; else x22 = x2;
//std::cout << "\n" << ++i << " " << x11 << " " << x22 << " " << fabs(x11 - x22) << "\n";
}
return (x11+x22)/2;
}
int main() {
double x0, y0, z0, b, n, eps = 1e-8;
//cout << "n = ";
cin >> n;
//cout << "x0 = ";
cin >> x0;
//cout << "y0 = ";
cin >> y0;
//cout << "z0 = ";
cin >> z0;
//cout << "b = ";
cin >> b;
d2(x0, y0, z0, b, n);
cout << "x_i \t y_i \t \t Y_i \t \t |Y_i - y_i|\n";
for (int i = 0; i <= 10; i++)
{
cout << x[i*int(n/10)] << "\t\t " << y[i*int(n/10)]
<< " \t\t " << Y(x[i*int(n/10)])
<< " \t " << fabs(Y(x[i*int(n/10)]) - y[i*int(n/10)]) << endl;
}
double zL, zL_;
zL_ = gs(x0, y0, b, n/2);
zL = gs(x0, y0, b, n);
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
<< "\nmin L = " << L(n) << endl;
while (fabs(zL-zL_) > eps)
{
zL_ = zL;n*=2;
zL = gs(x0, y0, b, n);
d2(x0, y0, zL, b, n);
cout << "\n=\nn = " << n << "\nmin zL = " << zL << "\nmin zL_ = " << zL_
<< "\nmin L = " << L(n) << endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWF0aC5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKZG91YmxlIHhbMTIwMDBdID0gezB9LCB5WzEyMDAwXSA9IHswfSwgelsxMjAwMF0gPSB7MH07Cgpkb3VibGUgZihkb3VibGUgeCwgZG91YmxlIHksIGRvdWJsZSB6KQp7CglyZXR1cm4gNip4Oy8vMip6LXkreC0yOwp9Cgpkb3VibGUgWShkb3VibGUgeCkKewoJZG91YmxlIEMxID0gMSwgQzIgPSAxOwoJcmV0dXJuIHBvdyh4LCAzKSArIEMxKnggKyBDMjsvL0MxKmV4cCh4KStDMip4KmV4cCh4KSt4Owp9Cgpkb3VibGUgTChpbnQgbikKewoJZG91YmxlIHMgPSAwOwoJZm9yIChpbnQgaSA9IDA7IGkgPCBuOy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PCAieF9pIFx0IHlfaSBcdCBcdCBZX2kgXHQgXHQgfFlfaSAtIHlfaXxcbiI7Cglmb3IgKGludCBpID0gMDsgaSA8PSAxMDsgaSsrKQoJewoJCWNvdXQgPDwgeFtpKmludChuLzEwKV0gPDwgIlx0XHQgIiA8PCB5W2kqaW50KG4vMTApXSAKCQk8PCAiIFx0XHQgIiA8PCBZKHhbaSppbnQobi8xMCldKSAKCQk8PCAiIFx0ICIgPDwgZmFicyhZKHhbaSppbnQobi8xMCldKSAtIHlbaSppbnQobi8xMCldKSA8PCBlbmRsOwoJfQoJZG91YmxlIHpMLCB6TF87Cgl6TF8gPSBncyh4MCwgeTAsIGIsIG4vMik7Cgl6TCA9IGdzKHgwLCB5MCwgYiwgbik7Cgljb3V0IDw8ICJcbj1cbm4gPSAiIDw8IG4gPDwgIlxubWluIHpMID0gIiA8LjIwPCB6TCA8PCAiXG5taW4gekxfID0gIiA8PCB6TF8KCQk8PCAiXG5taW4gTCA9ICIgPDwgTChuKSA8PCBlbmRsOwoJd2hpbGUgKGZhYnMoekwtekxfKSA+IGVwcykKCXsKCQkKCQl6TF8gPSB6TDtuKj0yOwoJCXpMID0gZ3MoeDAsIHkwLCBiLCBuKTsKCQlkMih4MCwgeTAsIHpMLCBiLCBuKTsKCQljb3V0IDw8ICJcbj1cbm4gPSAiIDw8IG4gPDwgIlxubWluIHpMID0gIiA8PCB6TCA8PCAiXG5taW4gekxfID0gIiA8PCB6TF8KCQk8PCAiXG5taW4gTCA9ICIgPDwgTChuKSA8PCBlbmRsOwoJCQoJfQoJCglyZXR1cm4gMDsKfQ==
prog.cpp: In function ‘int main()’:
prog.cpp:97:44: error: no match for ‘operator<’ (operand types are ‘std::basic_ostream<char>’ and ‘double’)
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
prog.cpp:97:44: note: candidate: operator<(int, double) <built-in>
prog.cpp:97:44: note: no known conversion for argument 1 from ‘std::basic_ostream<char>’ to ‘int’
In file included from /usr/include/c++/6/bits/stl_algobase.h:64:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_pair.h:437:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^~~~~~~~
/usr/include/c++/6/bits/stl_pair.h:437:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::pair<_T1, _T2>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:298:5: note: candidate: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator<(const reverse_iterator<_Iterator>& __x,
^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:298:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:355:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator<(const reverse_iterator<_IteratorL>& __x,
^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:355:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:1137:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
operator<(const move_iterator<_IteratorL>& __x,
^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:1137:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/stl_iterator.h:1143:5: note: candidate: template<class _Iterator> bool std::operator<(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
operator<(const move_iterator<_Iterator>& __x,
^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:1143:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::move_iterator<_IteratorL>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/string:52:0,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/basic_string.h:5150:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/include/c++/6/bits/basic_string.h:5150:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/string:52:0,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/basic_string.h:5163:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~~~~~
/usr/include/c++/6/bits/basic_string.h:5163:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: ‘std::basic_ostream<char>’ is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/string:52:0,
from /usr/include/c++/6/bits/locale_classes.h:40,
from /usr/include/c++/6/bits/ios_base.h:41,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/bits/basic_string.h:5175:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<(const _CharT* __lhs,
^~~~~~~~
/usr/include/c++/6/bits/basic_string.h:5175:5: note: template argument deduction/substitution failed:
prog.cpp:97:45: note: mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
^~~
In file included from /usr/include/c++/6/bits/ios_base.h:46:0,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from prog.cpp:1:
/usr/include/c++/6/system_error:200:3: note: candidate: bool std::operator<(const std::error_code&, const std::error_code&)
operator<(const error_code& __lhs, const error_code& __rhs) noexcept
^~~~~~~~
/usr/include/c++/6/system_error:200:3: note: no known conversion for argument 1 from ‘std::basic_ostream<char>’ to ‘const std::error_code&’
/usr/include/c++/6/system_error:274:3: note: candidate: bool std::operator<(const std::error_condition&, const std::error_condition&)
operator<(const error_condition& __lhs,
^~~~~~~~
/usr/include/c++/6/system_error:274:3: note: no known conversion for argument 1 from ‘std::basic_ostream<char>’ to ‘const std::error_condition&’
prog.cpp:97:53: error: invalid operands of types ‘double’ and ‘const char [12]’ to binary ‘operator<<’
cout << "\n=\nn = " << n << "\nmin zL = " <.20< zL << "\nmin zL_ = " << zL_
~~~^~~~~~~~~~~~~~~~~