如何直接从 Solve 的输出创建函数 [英] How to create a function directly from the output of Solve

查看:16
本文介绍了如何直接从 Solve 的输出创建函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我计算 Solve[f[x,y]==0,x],我会得到一堆解,比如:

If I evaluate Solve[f[x,y]==0,x], I get a bunch of solutions like:

{{x ->g[y]}, {x ->别的东西}},等等.

现在我想将每个 x->somethings 转换成一个函数.通常,我的要求很低,我的函数 f[x] 至多是一个三次方,为 x 提供了简单的解决方案.所以我总是手动定义 g1[y_]:=somethingg2[y_]:=... 等.

Now I want to convert each of those x->somethings into a function. Typically, my requirements are low, and my function f[x] is at the most a cubic, with straightforward solutions for x. So I've always just defined g1[y_]:=something, g2[y_]:=... etc, manually.

然而,对于我现在拥有的一个函数,Solve 输出一个复杂的多项式,运行 4 页长,并且有 4 个这样的解决方案.我曾尝试使用 SimplifyCollectFactor 等将形式简化为更简单的形式,但它似乎无法简化.

However, for a function that I have now, Solve outputs a complicated polynomial running 4 pages long, and there are 4 such solutions. I've tried reducing to simpler forms using Simplify, Collect, Factor etc, but it just seems irreducible.

有没有办法可以自动将它们分配给功能?(滚动页面并复制每一页非常困难......我必须寻找下一页的开始!)

Is there a way I can automatically assign them to functions? (It's extremely hard to scroll through pages and copy each one... and I have to look for where the next one begins!)

类似于:{g1[y_], g2[y_], g3[y_]} = Solve 的输出?

推荐答案

看来 Simon 打败了我的答案(我很高兴 StackOverflow 给了我一个弹出窗口让我知道!),因此我会采取不同的方法.您应该知道如何直接使用 Solve 的输出,因为多次这样做会很方便.

It appears Simon beat me to an answer (I am glad that StackOverflow gives me a pop-up to let me know!), therefore I will take a different approach. You should know how to use the output of Solve directly, as quite a few times it will be convenient to do that.

开头

ClearAll[a, x, sols]

sols = Solve[x^2 + a x + 1 == 0, x]

您可以执行以下操作.

x /. sols /. a -> 7

<小时>

绘制解决方案

Evaluate 在这里使用不是出于基本功能的需要,而是为了让 Plot 函数可以单独设置每个解决方案的样式


Plot the solutions

Evaluate is used here not out of necessity for basic function, but to allow the Plot function to style each solution separately

Plot[Evaluate[x /. sols], {a, 1, 4}]

注意这里使用 = 而不是 :=

Notice the use of = rather than := here

g[a_] = x /. sols[[2]]

<小时>

这是 Simon 为每个解决方案定义函数的方法的替代方法

MapIndexed[(gg[#2[[1]]][a_] := #) &, x /. sols]

然后将函数与语法 gg[1][17] 一起使用以表示第一个解决方案,并且 a == 17

The function is then used with the syntax gg[1][17] to mean the first solution, and a == 17

Plot[gg[1][a], {a, 1, 4}]

gg[2] /@ {1, 2, 3}

<小时>

这些用途通常要求 a(在本例中)保持未分配.


These uses do generally require that a (in this example) remain unassigned.

这篇关于如何直接从 Solve 的输出创建函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆