Matlab将符号变量转换为数组

时间:2019-07-02 10:06:07

标签: matlab symbols symbolic-math

我想将根据符号变量x定义的函数转换为数组。我的MWE是:

syms x
f = x.^2;
x = linspace(-10,10,100);
f1 = double(f);

我尝试使用double命令,但出现错误

Error using symengine
Unable to convert expression into double array.

Error in sym/double (line 692)
        Xstr = mupadmex('symobj::double', S.s, 0);

我不确定这意味着什么以及如何解决。

2 个答案:

答案 0 :(得分:2)

  • private void loadspots() { StringRequest stringRequestspots = new StringRequest(Request.Method.GET, URL_SPOTS, new Response.Listener<String>() { @Override public void onResponse(String response) { try { //converting the string to json array object spotsArray = new JSONArray(response); editor.putString("spotsArray", spotsArray.toString() ); editor.apply(); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); //adding our stringrequest to queue Volley.newRequestQueue(this).add(stringRequestspots); 的给定值替换之前,使用x double()
  • subs()具有相似的功能

  • vpa()相比,使用vpa()的优势在于您可以设置 自己十进制精度

代码如下

double()

  • syms x f = x.^2; x = linspace(-10,10,10); f1 = subs(f); f2 = double(f1) f3 = vpa(f1, 8) 输出: 小数点后的固定位数
double()

  • f2 = [100.0000 60.4938 30.8642 11.1111 1.2346 1.2346 11.1111 30.8642 60.4938 100.0000] 输出: 小数点后可调整的位数 ,此处精度调整为8
vpa()

答案 1 :(得分:1)

有几种方法可以做到这一点。以symfun开头:

syms x
f = symfun(x.^2, x);
xv = linspace(-10,10,100);
f1 = double(f(xv));

这等效于:

syms x       % or: syms f(x)
f(x) = x.^2;
xv = linspace(-10,10,100);
f1 = double(f(xv));

替代方法正在使用Adam建议的subs方法。