# error using symengine is generated while getting the derivative of equation of 8 degree with variable constants

1 vue (au cours des 30 derniers jours)
zein le 6 Jan 2022
Modifié(e) : Dyuman Joshi le 27 Déc 2023
I am trying to get the slope of equation (y3c=p1*xs^8 + p2*xs^7 + p3*xs^6 + p4*xs^5 + p5*xs^4 + p6*xs^3 + p7*xs^2 + p8*xs + p9),
where,xs=(x3s-mean)./dev; and xs x3s are symbols.
The problem is that the constant p1 to p9 ,mean and dev are arrays.
how should i modify the code to work without the following error
Error using symengine
Matrix must be square.
Error in sym/privBinaryOp (line 1034)
Error in ^ (line 339)
B = privBinaryOp(A, p, 'symobj::mpower');
Error in trailing_edge_2 (line 79)
y3c=p1*xs^8 + p2*xs^7 + p3*xs^6 + p4*xs^5 + p5*xs^4 + p6*xs^3 + p7*xs^2 + p8*xs + p9;
clc
clear
x=Excel(:,1);%x
y=Excel(:,2);%y
%%%%%%%%%%%%trailing edge(551:614,1:36))%%%%%%%%%%%
x3=[x(551:614);x(1:36)];
y3=[y(551:614);y(1:36)];
x3=x3';
y3=y3';
for i=1:length(x3)
if i<=33
mean(i)=-0.0005415;dev(i)=0.0007397;
p1(i) = -8.172e-05;p2(i) = -0.0004409 ; p3(i) = -0.0006484;
p4(i) = 0.000167;p5(i) = 0.0008287; p6(i) = 1.85e-05;
p7(i) = -0.0006773;p8(i) = -0.001281; p9(i) = 0.004286;
elseif (i>=34) && (i<=39)
mean(i)=7.32e-05;dev(i)=0.0001512;
p1(i) = 5.645e-05 ;p2(i) = 0.000194 ;p3(i) = 7.821e-05 ;
p4(i) = -0.0002781 ;p5(i) = -0.000149 ;p6(i) = 0.0002006 ;
p7(i) = 0.0002051 ;p8(i)= 0.0005569 ;p9(i) = 0.0002695 ;
else
mean(i)=-0.002376; dev(i)=0.002098;
p1(i) = 1.409e-05 ;p2(i) = 8.773e-05 ;p3(i) = 0.0001679 ;
p4(i) = 5.782e-05 ;p5(i) = -8.276e-05 ;p6(i) = 0.000105 ;
p7(i) = 0.0006627 ;p8(i) = 0.001226 ;p9(i) = -0.002926 ;
end
x3p(i)=(x3(i)-mean(i))/dev(i);
y3p(i)=p1(i)*x3p(i)^8 + p2(i)*x3p(i)^7 + p3(i)*x3p(i)^6 + p4(i)*x3p(i)^5 + p5(i)*x3p(i)^4 + p6(i)*x3p(i)^3 + p7(i)*x3p(i)^2 + p8(i)*x3p(i) + p9(i);%curve plotted using equ.
end
diff22=max(y3-y3p);
syms xs x3s
xs=(x3s-mean)./dev;
y3c=p1*xs^8 + p2*xs^7 + p3*xs^6 + p4*xs^5 + p5*xs^4 + p6*xs^3 + p7*xs^2 + p8*xs + p9;
dys=diff(y3c);
slope=double(vpa(subs(dys,x3s,x3)));
theta=atand(slope); %%% slope array
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Dyuman Joshi le 27 Déc 2023
Modifié(e) : Dyuman Joshi le 27 Déc 2023
Do not use built-in functions as names for variables (or scripts for that matter). Rename the variable to meanX or meanArray or something else.
If the constants are arrays, loop through the values.
I don't understand why you have defined xs as a symbolic variable, just to overwrite it in the next line.
Not to mention, it's not clear what you are substituting in place x3s, nor why you are substituting when dys does not depend of x3s.
Also, consider preallocating arrays and storing values/data in arrays instead of dynamically naming variables - TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)

Connectez-vous pour commenter.

### Réponses (1)

Raghav Bansal le 27 Déc 2023
Modifié(e) : Raghav Bansal le 27 Déc 2023
Hi Zein,
As per my understanding you facing error "Matrix must be square" error while executing the code.
The error you are encountering is due to attempting to perform symbolic operations on arrays with non-scalar operations, which is not supported in MATLAB. To resolve this issue, you need to modify the code to handle each element of the arrays individually within the symbolic expressions.
Use element-wise power '.^' instead of matrix power '^' when working with arrays. This will apply the power operation to each element of the array individually rather than trying to perform matrix operations.
Hope it helps!
Regards,
Raghav
##### 3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Raghav Bansal le 27 Déc 2023
Hi Dyuman,
I understand your point, but the error "Matrix must be square" arises when we try to apply scalar operations on a non-square matrix or arrays in this case. For example:
x = [1, 2];
x = x ^ 2
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To operate on each element of the matrix individually, use POWER (.^) for elementwise power.
will result in error while:
x = [1, 2; 1, 2];
x = x ^ 2
x = [1, 2];
x = x .^ 2
will execute without any error.
Thanks!
Raghav
Dyuman Joshi le 27 Déc 2023
Modifié(e) : Dyuman Joshi le 27 Déc 2023
I am aware of that. But the error OP got is not related to that, the error is related to an operation performed symbolically.
However, since I am not sure what OP has written in their code, I retracted my comment about that, but my point still stands.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Formula Manipulation and Simplification dans Help Center et File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by