I cannot remove 0 elements of a symbolic array

조회 수: 3 (최근 30일)
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022년 2월 1일
댓글: Mohammad Shojaei Arani 2022년 2월 1일
Hello friends!
Consider the following commands
syms x sigma(x)
par=sym('par%d', [1 2],'positive');
sigma(x)=par(1)+par(2)*x;
D_sigma=[sigma diff(sigma) diff(sigma,2) diff(sigma,3)];
D_sigma
nonzeros(D_sigma)
D_sigma(x) =
[par1 + par2*x, par2, 0, 0]
ans(x) =
[par1 + par2*x, par2, 0, 0]
I really have no idea why this does not work!!!!!!!
Any help!
Thanks in advance!
Babak

채택된 답변

Walter Roberson
Walter Roberson 2022년 2월 1일
syms x sigma(x)
par=sym('par%d', [1 2],'positive');
sigma(x)=par(1)+par(2)*x;
D_sigma=[sigma diff(sigma) diff(sigma,2) diff(sigma,3)];
D_sigma
D_sigma(x) = 
DSX = D_sigma(x);
nonzeros(DSX)
ans = 
The reason you had a problem is that your D_sigma is a symbolic function, not a vector.
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 2월 1일
Your D_sigma was a single function that returns a vector. The function itself is not zero, so nonzeros() does not remove the function.
You should not think of your D_sigma as being a vector of functions. It cannot be indexed into. It is a 1 x 1 object that happens to return a vector (whos size depends upon the input). [Huh, it can return a cell array???)
syms x sigma(x)
par=sym('par%d', [1 2],'positive');
sigma(x)=par(1)+par(2)*x;
D_sigma=[sigma diff(sigma) diff(sigma,2) diff(sigma,3)]
D_sigma(x) = 
D_sigma([-1 1])
ans = 1×4 cell array
{[par1 - par2 par1 + par2]} {[par2 par2]} {[0 0]} {[0 0]}
Mohammad Shojaei Arani
Mohammad Shojaei Arani 2022년 2월 1일
Thanks Walter for your kins explanation!
Although I am a mathematician I found coding a bit annoying but will improve over time!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by