Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:904ead7b9c3a, committed 2021-12-22
- Comitter:
- Kotttaro
- Date:
- Wed Dec 22 12:42:04 2021 +0000
- Child:
- 1:5ec4a7674069
- Commit message:
- 2021.12.22;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Dec 22 12:42:04 2021 +0000
@@ -0,0 +1,100 @@
+#include "mbed.h"
+#define ITMAX 100
+#define CGOLD 0.3819660
+#define SHIFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
+#define ZEPS 1.0e-10
+Serial pc(USBTX,USBRX);
+double f(double x);
+double brent(double min,double mid,double max,double tol);
+double SIGN(double x,double y);
+
+double brent(double min,double mid,double max,double tol)
+{
+ int iter;
+ double a,b,d,etemp,fu,fv,fw,fx,p,q,r,tol1,tol2,u,v,w,x,xm,xmin;
+ double e=0.0;
+ a=(min < max ? min : max);
+ b=(min > max ? min : max);
+ x=w=v=mid;
+ fw=fv=fu=f(x);
+ for(iter=1;iter<=ITMAX;iter++)
+ {
+ xm=0.5*(a+b);
+ tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
+ if(fabs(x-xm)<=(tol2-0.5*(b-a)))
+ {
+ xmin=x;
+ return xmin;
+ }
+ if(fabs(e)>tol1)
+ {
+ r=(x-w)*(fx-fv);
+ q=(x-v)*(fx-fw);
+ p=(x-v)*q-(x-w)*r;
+ q=2.0*(q-r);
+ if(q>0.0)p=-p;
+ q=fabs(q);
+ etemp=e;
+ e=d;
+ if(fabs(p)>=fabs(0.5*q*etemp)||p<=q*(a-x)||p>=q*(b-x))
+ { d=CGOLD*(e= (x>=xm ? a-x : b-x));}
+ else
+ {
+ d=p/q;
+ u=x+d;
+ if(u-a < tol2 || b-u < tol2)
+ {d=SIGN(tol1,xm-x);}
+ }
+ }
+ else
+ {
+ d=CGOLD*(e= (x>=xm ? a-x : b-x));
+ }
+ u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
+ fu=f(x);
+ if(fu <= fx)
+ {
+ if(u >= x)a=x; else b=x;
+ SHIFT(v,w,x,u);
+ SHIFT(fv,fw,fx,fu);
+ }
+ else{
+ if(u < x){a=u;}
+ else {b=u;}
+ if(fu <= fw || w==x)
+ {
+ v=w;
+ w=u;
+ fv=fw;
+ fw=fu;
+ }
+ else if (fu <= fv || v==x || v==w)
+ {
+ v=u;
+ fv=fu;
+ }
+ }
+
+ }
+ return xmin;
+ }
+//極小値を求めたい関数を定義
+double f(double x){
+ double x_return;
+ x_return=x*x;
+ return x_return;
+ }
+double SIGN(double x,double y)
+{
+ double x_return;
+ x_return=abs(x);
+ if(y<0.0)x_return=-x_return;
+ printf("f");
+ return x_return;
+ }
+int main() {
+ double x;
+ x= brent(-100.0,10.0,100.0,0.0000000001);
+ printf("%lf\r\n",x);
+ return 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Dec 22 12:42:04 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file