How to evaluate a symbolic expression having `max` and `diff`?
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
I have calculated the jacobian of two functions where variables are x1, x2, x3.
The jacobian is as follows-
JacobianF =
[                                            diff(max([0, (7*sin(4*pi*x1))/10], [], 2, 'omitnan', ~in(x1, 'real')), x1) + 96*pi*cos(6*pi*x1)*(x3 + sin(6*pi*x1)) + 160*3^(1/2)*pi^2*cos(6*pi*x1)*sin((3^(1/2)*pi*(20*x3 + 20*sin(6*pi*x1)))/3) + 1,                                                                                                                                                                                        0, 16*x3 + 16*sin(6*pi*x1) + diff(max([0, (7*sin(4*pi*x1))/10], [], 2, 'omitnan', ~in(x1, 'real')), x3) + (80*3^(1/2)*pi*sin((3^(1/2)*pi*(20*x3 + 20*sin(6*pi*x1)))/3))/3]
[diff(max([0, (7*sin(4*pi*x1))/10], [], 2, 'omitnan', ~in(x1, 'real')), x1) - 96*pi*cos((2*pi)/3 + 6*pi*x1)*(x2 - sin((2*pi)/3 + 6*pi*x1)) - 240*2^(1/2)*pi^2*cos((2*pi)/3 + 6*pi*x1)*sin((2^(1/2)*pi*(20*x2 - 20*sin((2*pi)/3 + 6*pi*x1)))/2) - 1, 16*x2 - 16*sin((2*pi)/3 + 6*pi*x1) + diff(max([0, (7*sin(4*pi*x1))/10], [], 2, 'omitnan', ~in(x1, 'real')), x2) + 40*2^(1/2)*pi*sin((2^(1/2)*pi*(20*x2 - 20*sin((2*pi)/3 + 6*pi*x1)))/2),                                                                                                                                                                      0]
Now, I need to evaluate this JacobianF at 
X = [0.2703    0.6193    0.9370];
where X(1) is x1 and so on.
To evaluate this JacobianF, I have used the following code-
Var_List = sym('x', [1, 3]);
df=double(subs(JacobianF, Var_List, X));
However, I get the following error. What is the cause of this error? How to resolve it and calculate the JacobianF at the specified position?
Error using symengine
Unable to convert expression containing remaining symbolic function calls into double array. Argument must be
expression that evaluates to number.
Error in sym/double (line 872)
Xstr = mupadmex('symobj::double', S.s, 0);
댓글 수: 12
  Torsten
      
      
 2024년 1월 2일
				Use
min(x,0) = 0.5*(x-abs(x))
as I used 
max(x,0) = 0.5*(x+abs(x))
below.
  Walter Roberson
      
      
 2024년 1월 4일
				Looks like it works for me when y is symbolic.
syms y
b_flat(y, 1, 2, 3)
b_flat(y, -10, 5, 17)
function Output = b_flat(y,A,B,C)
    Output = A+piecewise(0<=floor(y-B),0,floor(y-B))*A.*(B-y)/B-piecewise(0<=floor(C-y),0,floor(C-y))*(1-A).*(y-C)/(1-C);
    Output = round(Output*1e4)/1e4;
end
답변 (2개)
  Walter Roberson
      
      
 2024년 1월 1일
        The derivative of max() is not generally defined.
You would probably have more success if you defined in terms of piecewise() instead of in terms of max()
댓글 수: 1
  Dyuman Joshi
      
      
 2024년 1월 4일
				I guess the Sym engine does not have the ability to recognise that the definition of max() can be broken into a piecewise definition, than a derivative can be calculated.
I wonder if that is possible to implement or not.
  Torsten
      
      
 2024년 1월 1일
        
      편집: Torsten
      
      
 2024년 1월 2일
  
      Use 
max(x,0) = 0.5*(abs(x)+x)
for real x.
syms x1
f1 = max([0, (7*sin(4*pi*x1))/10], [], 2, 'omitnan', ~in(x1, 'real'))
f2 = 0.5*(abs(7*sin(4*pi*x1)/10)+7*sin(4*pi*x1)/10)
figure(1)
hold on
fplot(f1,[-0.5 0.25])
fplot(f2,[-0.5 0.25])
hold off
df1 = diff(f1,x1)
df2 = diff(f2,x1)
figure(2)
%fplot(df1,[-0.5 0.25])
fplot(df2,[-0.5 0.25])
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


















