Solving nonlinear equation involving sum
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to solve a nonlinear equation.
Code
syms g y z n
x = 0.0585;
y = 0.01;
Pi = sym(pi);
eqn = 1/g-sqrt(1 + z.^2/((2*n+1)*Pi*y + 4.4*Pi*x*g).^2) == 0;
sol = solve(eqn, g, 'returnconditions', true, 'maxdegree', 4);
G = simplify(sol.g)
Above gives me four root as suggested in https://www.mathworks.com/matlabcentral/answers/829308-solutions-are-only-valid-under-certain-conditions?s_tid=prof_contriblnk by Walter Robertson.
My code to solve nonlinear equation
sumArg4 = @ (n,z) 2*pi*(2*n+1)*pi*(G(4)-1)/((2*n+1)*pi + 4.4*pi*x/y)/((2*n+1)*pi + 4.4*pi*x*G(4)/y) + 0.6431;
Here only one out of above four roots satisfies so I took fourth roots (have done those calculation in Mathematica)
mytrial4 = @ (z) symsum(sumArg4 , n, 0, 10000)
%Solving nonlinear equation for z
zsol = fsolve(mytrial4, 2.28)
I read some of the posts where they say fsolve can't be used for symbolic expression.
Running above code gave me an error
function_handle with value:
@(z)symsum(sumArg4,n,0,10000)
Error using fsolve (line 309)
FSOLVE requires all values returned by functions to be of data type double.
Error in solveg_new_gz (line 24)
zsol = fsolve(mytrial4, 2.28)
So could you please suggest me how to take care of this error?
I have done in Mathematica but would like to try in Matlab as well.
Thank you
0 个评论
回答(1 个)
Ildeberto de los Santos Ruiz
2021-7-21
It will take you a long time to evaluate the sum from n = 0 to n = 10000.
You can solve it in a short time considering only from n = 0 to n = 10, although it is also necessary to define the equation as a numerical function handle.
% ...
mytrial4 = @(z) double(eval(symsum(sumArg4, n, 0, 10)))
zsol = fsolve(mytrial4, 2.28)
1 个评论
Walter Roberson
2021-7-22
Do not eval() a symbolic expression. The language of symbolic expressions displayed to the user is not MATLAB and is not MuPad (the internal symbol engine). Use subs() if you must.
In some cases you can use matlabFunction to speed up computation.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!