How to use symsum function inside a function

조회 수: 24 (최근 30일)
Chandra Sekhar Kommineni
Chandra Sekhar Kommineni 2022년 1월 9일
댓글: Chandra Sekhar Kommineni 2022년 1월 9일
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