Python 板


LINE

※ 引述《adnodnya (N.D.)》之铭言: : 我试图想要求一个方程式的图形 : f(x)= x ^(2/3) : 念做: x的三分之二次方 (怕有误解@@") : 我找了一下math模组发现 : math.pow(x,y) : y值说一定要是整数 : 所以不能用math(x,2/3) : 而math.sqrt(x) : 又不能开奇数方根 : 请问版上的高手们, 有没有其他可以替代的写法或模组? : 拜托了~~~感谢~~~ 以下是我修改网友的程式 不全是我写的 程式目的: 输出一个f(x)= 2x-3x^(2/3) 的图形 #======================================================== #!/usr/bin/env python #coding=utf-8 import math import cmath import Image import ImageDraw class DrawFun(): #画布像素尺寸 width = 512 height = 512 #X取值范围 min = -10 max = 10 #X的布长值 step = 1 #X的单位值像素 unit = 1 #画板对象 canvas = None #画笔对象 draw = None #画布中心值偏移值 ctrx = 0 ctry = 0 def __init__(self, width, height, min, max, step, unit): self.width = width self.height = height self.min = min self.max = max self.step = step self.unit = unit #创建画板对象 self.canvas = Image.new("RGB", [self.width,self.height]/ ,(255,255,255)) #创建画笔对象 self.draw = ImageDraw.Draw(self.canvas) #撷取画布中心点 self.ctrx = math.floor(self.width/2) self.ctry = math.floor(self.height/2) #绘制中心轴线 self.draw.line([(0,self.ctry),(self.width,self.ctry)],/ fill =(0,0,0)) self.draw.line([(self.ctrx,0),(self.ctrx,self.height)],/ fill =(0,0,0)) #显示单位格数 左右对称计算 viewx = int(math.floor((self.width/self.unit + 1)/2)) viewy = int(math.floor((self.height/self.unit + 1)/2)) #绘制单元格 for x in range(1,viewx+1): offsetx = x * self.unit self.draw.line([(self.ctrx + offsetx,0),(self.ctrx / + offsetx,self.height)], fill =(200,200,200)) self.draw.line([(self.ctrx - offsetx,0),(self.ctrx / - offsetx,self.height)], fill =(200,200,200)) for y in range(1,viewy+1): offsety = y * self.unit self.draw.line([(0,self.ctry + offsety),(self.width,/ self.ctry + offsety)], fill =(200,200,200)) self.draw.line([(0,self.ctry - offsety),(self.width,/ self.ctry - offsety)], fill =(200,200,200)) def paint(self, fun, color): #结果点集 aryPoint = [] #转换为1为布长 min = int(math.floor(self.min/self.step)) max = int(math.floor(self.max/self.step)) for i in range(min, max): #实际函数值 x = i * self.step y = fun(x) #笛卡尔座标系到屏幕座标系 #比例变换 x = x * self.unit y = y * self.unit #座标变换 curX = self.ctrx + x curY = self.ctry - y #保存座标到数组 aryPoint.append((curX,curY)) #绘制函数 self.draw.point(aryPoint,fill = color) #储存到PNG def saveToPNG(self, path): self.canvas.save(path,"PNG") def test(x): ########################################################### abc = 2*x-3*(math.pow(x,2/3)) return abc ########################################################### if __name__ == "__main__": #DrawFun("画布宽度","画布高度","x最小值","x最大值",/ "x步长值","x单位值像素大小") drawfun = DrawFun(512, 512, -10, 10, 0.001, 50) #DrawFun.paint(f(x)函数,RGB颜色) drawfun.paint(test,(0,0,255)) #储存图片 drawfun.saveToPNG("demo.png") #======================================================== python math的模组叙述说 math.pow(x,y) y 只能接受整数值 所以现在卡关卡在这 不知道有什麽方法可以求的立方根 解决x的2/3次方 拜托各位高手大大了~~~ --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.127.220.24
1F:→ ya790206:y也可以接受浮点数... math.pow(27,1/3.0)=>3.0 11/08 18:20
2F:→ ya790206:你的问题在於x为负数时,他无法开立方。 11/08 18:21
3F:→ ya790206:改成math.pow(x**2,1/3.0)看看 11/08 18:22
4F:推 suzuke:推楼上,这样就可以避免掉负数的问题~ 11/08 21:18
5F:→ adnodnya:哇!!!好威喔!!! 原来是这麽回事~ 感谢大大们~~~ 11/09 01:12







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP