fzero function not working with symsum

조회 수: 9 (최근 30일)
Iver Brekken
Iver Brekken 2022년 2월 7일
편집: Walter Roberson 2022년 2월 8일
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