fzero function not working with symsum

9 次查看(过去 30 天)
clear all
clc
close all
y = 0.5;
u = 0.25;
syms n t
fun = @(t) (-u + y-2/pi*symsum(1/n*sin(n*pi*(1-y))*exp(-(n*pi)^2*t),n,1,inf));
fzero(fun,0)
I want to find the root of the following (variable: t). I get the following error message:
Error using num2str (line 47)
Input to num2str must be numeric.
Error in fzero>errHandler (line 581)
sprintf('%g',y),num2str(fy)));
Error in fzero (line 346)
[b,fval,exitflag,output] = errHandler(a,fa,intervaliter,iter,fcount,trace,buildOutputStruct);
Error in a4_1 (line 9)
fzero(fun,0)
What can I do different?

回答(1 个)

Walter Roberson
Walter Roberson 2022-2-7
Your symsum() is returning a symbolic value, so your fun() is returning a symbolic value -- but fzero requires the function to return single or double.
You should double() the results of the symsum.
  2 个评论
Iver Brekken
Iver Brekken 2022-2-7
clear all
clc
close all
y = 0.5;
u = 0.25;
syms n x
s = double(symsum(1/n*sin(n*pi*(1-y))*exp(-(n*pi)^2*x),n,1,inf));
fun = -u + y-2/pi*s;
fzero(fun,0)
Now i get this error message:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
Error in sym/double (line 702)
Xstr = mupadmex('symobj::double', S.s, 0);
Error in a4_1 (line 8)
s = double(symsum(1/n*sin(n*pi*(1-y))*exp(-(n*pi)^2*x),n,1,inf));
Walter Roberson
Walter Roberson 2022-2-8
编辑:Walter Roberson 2022-2-8
format long g
y = 0.5;
u = 0.25;
syms n x
s = symsum(1/n*sin(n*pi*(1-y))*exp(-(n*pi)^2*x),n,1,inf)
s = 
fun = -u + y-2/pi*s
fun = 
Fun = matlabFunction(fun, 'vars', x)
Fun = function_handle with value:
@(x)feval(@(n)symsum((exp(-n.^2.*x.*pi.^2).*sin((n.*pi)./2.0))./n,n,1.0,Inf),sym('n')).*(-6.366197723675814e-1)+1.0./4.0
FunW = @(x) double(Fun(x))
FunW = function_handle with value:
@(x)double(Fun(x))
location = fzero(FunW, [.09 .1])
location =
0.0946869595678489

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by