How to use symsum function inside a function

26 次查看(过去 30 天)
Below I'm trying to write a small function, but I'm getting following error message
Unable to create a symbolic variable 'j' by using 'syms' inside a MATLAB function because 'j' is an existing function name, class name, method name, and so on. Use 'sym' instead.
the code is working outside the function module, I don't know the exact reason behind this? could someone explain me how to rectify this?
Thank yyou so much in advance
function [Area sigma]= Fractalblock(L0,t0,Rt,N,T);
syms j
Area= (L0*t0*symsum((4^(j+1))*((0.5)^j)*(Rt^j),j,0,N-1))-((t0^2)*symsum(2^(2*j+1)*(Rt^((2*j)-1)),j,1,N-1))-((t0^2)*symsum(2^(2*j+2)*Rt^(2*j),j,0,N-1));
sigma= area/T^2;
end

采纳的回答

Walter Roberson
Walter Roberson 2022-1-9
I have not seen that message before, but the work-around is
function [Area sigma]= Fractalblock(L0,t0,Rt,N,T);
j = sym('j');
Area= (L0*t0*symsum((4^(j+1))*((0.5)^j)*(Rt^j),j,0,N-1))-((t0^2)*symsum(2^(2*j+1)*(Rt^((2*j)-1)),j,1,N-1))-((t0^2)*symsum(2^(2*j+2)*Rt^(2*j),j,0,N-1));
sigma= area/T^2;
end
You could have the same problem for i, j, pi, and eulergamma and possibly other names
syms does a hidden assignment to a variable, and when there are hidden assignments to a variable name that happens to be the same as the name of a function, then MATLAB is permitted to treat the name as a function, because MATLAB does static analysis instead of dynamic analysis.

更多回答(0 个)

类别

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

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by