Re: [問題] power math.pow(x,y)

看板Python作者 (N.D.)時間14年前 (2010/11/08 05:43), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串2/2 (看更多)
※ 引述《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

11/08 18:20, , 1F
y也可以接受浮點數... math.pow(27,1/3.0)=>3.0
11/08 18:20, 1F

11/08 18:21, , 2F
你的問題在於x為負數時,他無法開立方。
11/08 18:21, 2F

11/08 18:22, , 3F
改成math.pow(x**2,1/3.0)看看
11/08 18:22, 3F

11/08 21:18, , 4F
推樓上,這樣就可以避免掉負數的問題~
11/08 21:18, 4F

11/09 01:12, , 5F
哇!!!好威喔!!! 原來是這麼回事~ 感謝大大們~~~
11/09 01:12, 5F
文章代碼(AID): #1Cruu3w6 (Python)
討論串 (同標題文章)
本文引述了以下文章的的內容:
問題
1
8
完整討論串 (本文為第 2 之 2 篇):
問題
1
8
文章代碼(AID): #1Cruu3w6 (Python)