[轉載] 【數學問題】利用python求解表達式

參考鏈接: Python 變量 |表達式 |條件和函數

有時候我們會遇到一些很復雜的表達式,或者想要求解某個表達式,但是手動計算的話不但耗時還費精力,我們能不能利用計算機來幫助我們進行計算呢??

1、問題描述?

我們昨天提出了四足機器人的初始姿態的問題,里面給出了很多公式,例如下面這條。:?

?

? ? ?

? ? ??

? ? ? ?

? ? ? ??

? ? ? ? ?

? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ? ??

? ? ? ? ? ? ?α

? ? ? ? ? ??

? ? ? ? ? ??

? ? ? ? ? ? ?=

? ? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ??

? ? ? ? ?

? ? ? ? ?

? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ? ??

? ? ? ? ? ? ?arccos

? ? ? ? ? ??

? ? ? ? ? ??

? ? ? ? ? ? ??

? ? ? ? ? ??

? ? ? ? ? ??

? ? ? ? ? ? ?

? ? ? ? ? ? ? (

? ? ? ? ? ? ?

? ? ? ? ? ? ?

? ? ? ? ? ? ??

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?l

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?1

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?2

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? +

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?L

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?2

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?l

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?2

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?2

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ?

? ? ? ? ? ? ??

? ? ? ? ? ? ??

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? 2

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?l

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ? ?1

? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? L

? ? ? ? ? ? ? ?

? ? ? ? ? ? ??

? ? ? ? ? ? ?

? ? ? ? ? ? ?

? ? ? ? ? ? ? )

? ? ? ? ? ? ?

? ? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ??

? ? ? ? ?

? ? ? ??

? ? ? ??

? ? ? ? ?

? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ??

? ? ? ? ?

? ? ? ??

? ? ? ?

? ? ? ?

? ? ? ? \begin{matrix} \alpha =& \arccos{\left(\frac{l_1^2 + L^2 -l_2^2}{2l_1L} \right)}\\ \\ \end{matrix}

? ? ? ?

? ? ??

? ? ?α=?arccos(2l1?Ll12?+L2?l22??)??

如果我們想要知道

? ??

? ? ?

? ? ??

? ? ? ?

? ? ? ? L

? ? ? ?

? ? ??

? ? ??

? ? ? ?L

? ? ??

? ? ?

? ? L關于

? ??

? ? ?

? ? ??

? ? ? ?

? ? ? ? α

? ? ? ?

? ? ??

? ? ??

? ? ? ?\alpha

? ? ??

? ? ?

? ? α的表達式,可能得經過一系列化簡才能得出答案,這時候我們可以借助強大的計算機來幫我們實現這些運算,最終得出答案:?

?

? ? ?

? ? ??

? ? ? ?

? ? ? ??

? ? ? ? ?L

? ? ? ??

? ? ? ??

? ? ? ? ?=

? ? ? ??

? ? ? ??

? ? ? ? ?

? ? ? ? ? l

? ? ? ? ?

? ? ? ? ?

? ? ? ? ? 1

? ? ? ? ?

? ? ? ??

? ? ? ??

? ? ? ? ?cos

? ? ? ??

? ? ? ??

? ? ? ? ??

? ? ? ??

? ? ? ??

? ? ? ? ?

? ? ? ? ? (

? ? ? ? ?

? ? ? ? ?

? ? ? ? ? α

? ? ? ? ?

? ? ? ? ?

? ? ? ? ? )

? ? ? ? ?

? ? ? ??

? ? ? ??

? ? ? ? ?+

? ? ? ??

? ? ? ??

? ? ? ? ?

? ? ? ? ??

? ? ? ? ? ??

? ? ? ? ??

? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ? ? l

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? 1

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? 2

? ? ? ? ? ?

? ? ? ? ??

? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ? ??

? ? ? ? ? ? ?sin

? ? ? ? ? ??

? ? ? ? ? ??

? ? ? ? ? ? ??

? ? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? 2

? ? ? ? ? ?

? ? ? ? ??

? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ? ? (

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? α

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? )

? ? ? ? ? ?

? ? ? ? ??

? ? ? ? ??

? ? ? ? ? ?+

? ? ? ? ??

? ? ? ? ??

? ? ? ? ? ?

? ? ? ? ? ? l

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? 2

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? 2

? ? ? ? ? ?

? ? ? ? ??

? ? ? ? ?

? ? ? ??

? ? ? ?

? ? ? ?

? ? ? ? L =l_1 \cos{\left(\alpha \right)} + \sqrt{- l_1^{2} \sin^{2}{\left(\alpha \right)} + l_{2}^{2}}

? ? ? ?

? ? ??

? ? ?L=l1?cos(α)+?l12?sin2(α)+l22?

? ? ? ? ? ??

? ? ? ? ? ? ?

? ? ? ? ? ? ??

這個關于

? ??

? ? ?

? ? ??

? ? ? ?

? ? ? ? L

? ? ? ?

? ? ??

? ? ??

? ? ? ?L

? ? ??

? ? ?

? ? L的表達式一看就很復雜,是作者手算出來的嗎?當然不是,我是利用smypy這個庫計算出來的表達式。?

sympy是一個Python的科學計算庫,用一套強大的符號計算體系完成諸如多項式求值、求極限、解方程、求積分、微分方程、級數展開、矩陣運算等等計算問題?

2、代碼?

這里直接給出代碼?

from sympy import *

import numpy as np

?

# 定義我們的符號

l1 = symbols('11')

l2 = symbols('l2')

L = symbols('L')

a = symbols('a')

?

# 定義等式,右邊要等于0

eq = cos(a) - (l1**2 + L**2 - l2**2)/(2*l1*L)

?

# 求解

result = solve(eq, L)

?

# 打印結果

print('L:', result)

?

# 將結果轉化成latex格式

print(latex(result))

?

?

輸出如下,因為是二次函數,所以這里會給出兩個解,實際上只有一個是符合實際情況的。?

L: [11*cos(a) - sqrt(-11**2*sin(a)**2 + l2**2), 11*cos(a) + sqrt(-11**2*sin(a)**2 + l2**2)]

\left[ 11 \cos{\left(a \right)} - \sqrt{- 11^{2} \sin^{2}{\left(a \right)} + l_{2}^{2}}, \? 11 \cos{\left(a \right)} + \sqrt{- 11^{2} \sin^{2}{\left(a \right)} + l_{2}^{2}}\right]

?

?

solve()函數第一個參數是一個方程式(等于零),第二個參數是想要求解的符號。?

3、求解一般代數方程?

from sympy.solvers import solve

from sympy import Symbol

x = Symbol('x')

result = solve(x**2 - 1, x)

?

[-1, 1]

?

更多使用方法可以參考官方文檔?

?

?

?如果覺得ok,點個贊,點個關注,也歡迎給個打賞支持一下編者的工作

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540574.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540574.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540574.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

cesium廣告牌_公路廣告牌

cesium廣告牌Description: 描述: This is a standard dynamic programing problem of finding maximum profits with some constraints. This can be featured in any interview coding rounds. 這是在某些約束條件下找到最大利潤的標準動態編程問題。 這可以在任何…

你和大牛差了啥

mmp。無時無刻不在想和大牛差在哪里了。別人為什么可以那么牛逼而你tmd那么菜!整個人頓時都頹廢了。啥事兒不想干。后來想了想感情就是他比較黑吧。

[轉載] python數組的使用

參考鏈接: Python中整數的最大可能值是多少? 原文地址為: python數組的使用 python數組的使用 python數組的使用 2010-07-28 17:17 1、Python的數組分三種類型: (1) list 普通的鏈表,初始化后可以通過特定方法…

scala中循環守衛_Scala中的循環

scala中循環守衛Scala中的循環 (Loops in Scala) In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language d…

50個必備基礎命令

1.tar創建一個新的tar文件$ tar cvf archive_name.tar dirname/解壓tar文件$ tar xvf archive_name.tar查看tar文件$ tar tvf archive_name.tar2. grep在文件中查找字符串(不區分大小寫)$ grep -i "the" demo_file輸出成功匹配的行,以及該行之后的三行$ g…

NM的完整形式是什么?

NM:無消息 (NM: No Message) NM is an abbreviation of "No Message". NM是“無消息”的縮寫。 It is an expression, which is commonly used in the Gmail platform. It is also written as N/M or n/m or *n/m*. It is written in the subject of the…

[轉載] python中全局變量和局部變量解析

參考鏈接: Python中的全局變量和局部變量 python函數中可以訪問全局變量但是不能給全局變量賦值,除非進行顯式聲明global a 比如定義了全局變量 a 在函數my_fun()中可以直接訪問a的值,而不需要global全局變量申明。下圖為上面代碼運行輸出 …

【iCore4 雙核心板_FPGA】例程十六:基于雙口RAM的ARM+FPGA數據存取實驗

實驗現象: 核心代碼: int main(void) {/* USER CODE BEGIN 1 */int i;int address,data;char error_flag 0;char receive_data[50];char buffer[8];char *p;/* USER CODE END 1 *//* MCU Configuration-----------------------------------------------…

[轉載] Python中TFTP的理解

參考鏈接: Python中的打包pack和拆包unpack參數 Num01–>TFTP協議介紹 TFTP(Trivial File Transfer Protocol,簡單文件傳輸協議) 是TCP/IP協議族中的一個用來在客戶端與服務器之間進行簡單文件傳輸的協議 特點: 1,簡單 2…

gn fast-gn_GN的完整形式是什么?

gn fast-gnGN:晚安 (GN: Good Night) GN is an abbreviation of "Good Night". GN是“ Good Night”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenge…

從零開始編寫自己的C#框架(27)——什么是開發框架

前言 做為一個程序員,在開發的過程中會發現,有框架同無框架,做起事來是完全不同的概念,關系到開發的效率、程序的健壯、性能、團隊協作、后續功能維護、擴展......等方方面面的事情。很多朋友在學習搭建自己的框架,很多…

[轉載] Python 遞歸 深入理解遞歸 Python遞歸剖析,絕對讓你看懂!

參考鏈接: Python | print()中的結束參數 目錄 遞歸剖析 遞歸的兩個過程 return 返回值 詳解 遞歸思路二分法和遞歸尾遞歸遞歸練習題 遞歸剖析 遞歸真的很重要,之前學的時候,學的一知半解,以為真正了解,每次想到遞歸…

laravel 項目遷移_在Laravel遷移

laravel 項目遷移Before moving forward we need to know some facts about it, 在繼續前進之前,我們需要了解一些事實, Resources: In these directories, we have already a js, lang, sass and view page. Where, sass and js file holf their uncom…

Python之list對應元素求和

本次分享將講述如何在Python中對多個list的對應元素求和,前提是每個list的長度一樣。比如:a[1,2,3], b[2,3,4], c[3,4,5], 對a,b,c的對應元素求和,輸出應為[6,9,12].    方法一:   直接求解,按照對應元素相加的…

[轉載] Python中str跟int的轉換

參考鏈接: Python中的類型轉換 字符串str轉換成int: int_value int(str_value) int轉換成字符串str: str_value str(int_value) a100 b666 #int轉str類型 print(int轉str類型) print(int轉str: str(a)) #str轉int類型 print(str轉int類型…

ot協議是什么_OT的完整形式是什么?

ot協議是什么OT:主題外 (OT: Off Topic) OT is an abbreviation of "Off Topic". OT是“ Off Topic”的縮寫 。 It is an expression, which is commonly used in Gmail or messaging platform. It shows that the email that has been sent is irrelev…

[轉載] python中字符串編碼形式及其所占字節

參考鏈接: Python中的字節對象與字符串 1.常見字符串編碼錯誤 在使用Python讀文件時經常遇到編碼問題引起的錯誤,比如: UnicodeDecodeError: gbk codec cant decode byte 0x80 in position 30: illegal multibyte sequence 遇到這種異…

[AtCoder-ARC073F]Many Moves

題目大意:   有一排n個格子和2枚硬幣。   現在有q次任務,每一次要你把其中一枚硬幣移到x的位置上,移動1格的代價是1。   兩枚硬幣不能同時移動,任務必須按次序完成。   現在告訴你兩枚硬幣初始狀態所在的位置a和b&#xf…

ScalavsKotlin

Is Scala better that Kotlin? No..., Is Kotlin better than Scala? No... Scala比Kotlin更好嗎? 不...,Kotlin勝過Scala嗎? 沒有... Both programming languages have their own profits and are for a specific set of development. It…

工業智能相機與基于PC的機器視覺的區別比較

隨著科技的日漸成熟,機器視覺得到了飛速發展。由于嵌入式技術的發展,近幾年智能相機性能顯著提高,越來越多必須依賴于PC處理的應用開始向智能相機平臺傾斜。低成本、高可靠性及易于安裝維護等優勢,使得機器視覺在制造業上的規模性應用越來越普…