TF随笔-14-二分法求解一元方程
来源:互联网 发布:斯诺克最新过百数据 编辑:程序博客网 时间:2024/06/07 14:04
tensorflow编程还是比较麻烦~
#!/usr/bin/env python2# -*- coding: utf-8 -*-"""Created on Mon Jul 24 08:25:41 2017f(x)=x^3+2*(x^2)-45=0二分法求解一元方程@author: myhaspl@myhaspl.com"""import tensorflow as tfdef fp(x): return tf.subtract(tf.add(tf.pow(x,3),tf.multiply(tf.pow(x,2),2.)),45.)i=tf.Variable(1,dtype=tf.int32)MIN_RESULT=-10.MAX_RESULT=10.a=tf.constant(MIN_RESULT,dtype=tf.float32)b=tf.constant(MAX_RESULT,dtype=tf.float32)n=tf.constant(300)result=tf.Variable([],dtype=tf.float32)x_result=tf.Variable(MIN_RESULT-1,dtype=tf.float32)fa_result=tf.Variable(fp(MIN_RESULT),dtype=tf.float32)TOL=1e-6#本博客所有内容是原创,如果转载请注明来源http://blog.csdn.net/myhaspl/ def fp_cond(i,n,a,b,result,fa_result,x_result): return tf.logical_and(tf.less(i,n),tf.less(x_result,MIN_RESULT))def fp_body(i,n,a,b,result,fa_result,x_result): p=tf.add(a,tf.divide((b-a),2.)) fp_result=fp(p) x_result=tf.cond(tf.logical_or(tf.equal(fp_result,0),tf.less((b-a)/2.,TOL)),lambda:p,lambda:MIN_RESULT-1) result=tf.concat([[a,b,x_result],result],axis=0) [a,b,fa_result]=tf.cond(tf.greater(tf.multiply(fa_result,fp_result),0),\ lambda:[p,b,fp_result],\ lambda:[a,p,fa_result]) i=i+1 return i,n,a,b,result,fa_result,x_resultinit_assign = tf.global_variables_initializer()i,n,a,b,result,fa_result,x_result=tf.while_loop(fp_cond,fp_body,\ loop_vars=[i,n,a,b,result,fa_result,x_result],\ shape_invariants=[i.get_shape(),n.get_shape(),a.get_shape(),b.get_shape(),tf.TensorShape([None]),fa_result.get_shape(),x_result.get_shape()]) with tf.Session() as sess: sess.run(init_assign) sess.run([i,n,a,b,result,fa_result,x_result]) print "在",sess.run(result[0]),"-",sess.run(result[1]),"内:" print sess.run(i),"次迭代,计算方程的解:",sess.run(result[2])
阅读全文
1 0
- TF随笔-14-二分法求解一元方程
- 一元方程求解
- 求解一元方程公式法
- 使用单变量求解求一元方程的解
- TF随笔-2
- tf随笔-1
- TF随笔-3
- TF随笔-4
- tf随笔-5
- tf随笔-6
- TF随笔-7
- TF随笔-8
- TF随笔-9
- TF随笔-10
- TF随笔-11
- TF随笔-12
- TF随笔-13
- 二分法求解函数值
- ACM-行编辑程序
- Android Touch事件传递机制通俗讲解
- js控制输入框最大长度
- Qt第一天——搭建开发环境
- hihocoder 1174 : 拓扑排序·一
- TF随笔-14-二分法求解一元方程
- 数据结构编程笔记二:第一章 绪论 三元组的程序实现
- redis面试总结
- 解决频繁升级Xcode问题
- js读取xml的<![CDATA[]]>存放内容
- TCP/IP, WebSocket 和 MQTT
- reselsense采集深度值
- 如何防止鼠标移出移入子元素触发mouseout和mouseover事件
- java集合vector与stack