已知三点,如何求y=ax^2+bx+c的方程表达式
本帖最后由 tryhi 于 2021-9-2 22:30 编辑已知三点(x1 y1)(x2 y2)(x3 y3)为方程y=ax^2+bx+c上的点,
编写一个函数求a、b、c
(defun tt (p1 p2 p3)
;……
(list a b c);返回a b c
)
续:迷你工具箱的作者阿迈用LM的求逆矩阵函数给出非常优秀的代码,可惜他本人没有来跟帖,代码如下
;; Matrix Inverse-gile & Lee Mac
;; Uses Gauss-Jordan Elimination to return the inverse of a non-singular nxn matrix.
;; Args: m - nxn matrix
;(invm '((2 0 0)(2 2 0)(0 0 2)))
(defun mat_inv ( m / c f p r )
(defun f ( p m ) (mapcar (function (lambda ( x ) (mapcar (function (lambda ( a b ) (- a (* (car x) b)))) (cdr x) p))) m))
(setqm (mapcar (function append) m (mat_o (length m))))
(while m
(setq c (mapcar (function (lambda ( x ) (abs (car x)))) m))
(repeat (vl-position (apply (function max) c) c)
(setq m (append (cdr m) (list (car m))))
)
(if (equal 0.0 (caar m) 1e-14)
(setq m nil r nil)
(setq
p (mapcar (function (lambda ( x ) (/ (float x) (caar m)))) (cdar m))
m (f p (cdr m))
r (cons p (f p r))
)
)
)
(reverse r)
)
;; Identity Matrix-Lee Mac
;; Args: n - matrix dimension
(defun mat_o ( n / i j l m )
(repeat (setq i n)
(repeat (setq j n)
(setq
l (cons (if (= i j) 1.0 0.0) l)
j (1- j)
)
)
(setq
m (cons l m)
l nil
i (1- i)
)
)
m
)
;;已知三点求抛物线的a b c常数
(defun try-y=ax^2+bx+c (p1 p2 p3 / x1 x2 x3 y1 y2 y3)
(mapcar 'set '(x1 y1 ) p1)
(mapcar 'set '(x2 y2 ) p2)
(mapcar 'set '(x3 y3 ) p3)
(mxv
(mat_inv
(list
(list (* x1 x1) x1 1)
(list (* x2 x2) x2 1)
(list (* x3 x3) x3 1)
)
)
(list y1 y2 y3)
)
)
(setq
p1 '(10000. 9999.650)
p2 '(20000. 19999.4790)
p3 '(0. 0.)
)
(mapcar '(lambda(x)(rtos x 2 16))(try-y=ax^2+bx+c p1 p2 p3))
本帖最后由 LPACMQ 于 2021-9-2 22:11 编辑
照着抄的,不知道对不对哈
用代入相消法 本帖最后由 mahuan1279 于 2021-9-3 06:39 编辑
可以直接写出方程式。
y=f(x)=y1*(x-x2)*(x-x3)/(x1-x2)/(x1-x3)+y2*(x-x1)*(x-x3)/(x2-x1)/(x2-x3)+y3*(x-x1)*(x-x2)/(x3-x1)/(x3-x2),
故c=f(0),
b=(f(1)-f(-1))/2,
a=f(1)-c-b
mahuan1279 发表于 2021-9-3 01:20
可以直接写出方程式。
y=f(x)=y1*(x-x2)*(x-x3)/(x1-x2)/(x1-x3)+y2*(x-x1)*(x-x3)/(x2-x1)/(x2-x3)+y3*(x ...
数学渣表示……怎么转成代码? 缺的函数
(defun mxv ( m v )
(mapcar '(lambda ( r ) (apply '+ (mapcar '* r v))) m)
) LPACMQ 发表于 2021-9-2 18:28
照着抄的,不知道对不对哈
百度害人! a值相差一个符号,然后后面的b,c的都是错的。例子他也就代入了一个值做检验。正确的答案是:
y=x^2-5x+6 感谢大佬们分享资料! 可能会因为数据问题(如病态矩阵)存在BUG吧。 mahuan1279 发表于 2021-9-7 15:18
可能会因为数据问题(如病态矩阵)存在BUG吧。
暂不考虑这个问题
页:
[1]