The following error occurred converting from sym to double: Unable to convert expression containing symbolic variables into double array Apply' subs' function first to substitute values for variables.

Hello,
When I try the loop for ii (see attached mix file) I get this error:
The following error occurred converting from sym to double: Unable to convert expression containing symbolic variables into double array Apply'subs' function first to substitute values ​​for variables.
It has solution because I tried writing each ii and the respective equations. But I need the loop (obviously) because I want to introduce several values ​​of ii and unknows to the system.
PD: Not so important but, as you can see, inside of the equation eqll, eqll. eqlll ... are the unknowns CO, C1 .... DO, D1, ... which are inside of a series (but I) couldnt find a way, so I wrote the elements of the serie) Is there any way to optimize that?

2 commentaires

clear
clc
% General input
h = 8;
l = 2;
d = 0.09;
sections = 5;
% Data from previous calculations
z = [0,-1.6,-3.2,-4.8,-6.4,-8];
P = [-0.923072381390763 + 0.083645843312860i,-0.763379411108664 + 0.047842477328015i,-0.647844785539475 + 0.001886672095714i,-0.569785303973405 - 0.037689986193477i,-0.524685547937092 - 0.062861417563918i,-0.509936682892244 - 0.071256842113728i];
Q = [0.000000000000000 + 0.100765147467902i,0.000000000000000 + 0.093981142166511i,0.000000000000000 + 0.095329989814646i,0.000000000000000 + 0.100156105292950i,0.000000000000000 + 0.099088979609004i,0.000000000000000 + 0.096724494080166i];
points = sections + 1;
eqI = zeros (1,points);
eqII = zeros (1,points);
eqIII = zeros (1,points);
syms C0 C1 C2 C3 C4 C5 C6 C7 C8 D0 D1 D2 D3 D4 D5 D6 D7 D8
for ii = 1:points
eqI(ii) = (1/h)*C0 + (2/h)*(C1*cos(1*pi*(z(1,ii)+h)/h)+ ...
C2*cos(2*pi*(z(1,ii)+h)/h)+ ...
C3*cos(3*pi*(z(1,ii)+h)/h)+ ...
C4*cos(4*pi*(z(1,ii)+h)/h)+ ...
C5*cos(5*pi*(z(1,ii)+h)/h)+ ...
C6*cos(6*pi*(z(1,ii)+h)/h)+ ...
C7*cos(7*pi*(z(1,ii)+h)/h)+ ...
C8*cos(8*pi*(z(1,ii)+h)/h))-d;
eqII(ii) = (1/h)*(C0+D0*l) + (2/h)*(((C1*cosh(1*pi*l/h) + D1*sinh(1*pi*l/h))*cos(1*pi*(z(1,ii)+h)/h)) ...
+((C2*cosh(2*pi*l/h) + D2*sinh(2*pi*l/h))*cos(2*pi*(z(1,ii)+h)/h)) ...
+((C3*cosh(3*pi*l/h) + D3*sinh(3*pi*l/h))*cos(3*pi*(z(1,ii)+h)/h)) ...
+((C4*cosh(4*pi*l/h) + D4*sinh(4*pi*l/h))*cos(4*pi*(z(1,ii)+h)/h)) ...
+((C5*cosh(5*pi*l/h) + D5*sinh(5*pi*l/h))*cos(5*pi*(z(1,ii)+h)/h)) ...
+((C6*cosh(6*pi*l/h) + D6*sinh(6*pi*l/h))*cos(6*pi*(z(1,ii)+h)/h)) ...
+((C7*cosh(7*pi*l/h) + D7*sinh(7*pi*l/h))*cos(7*pi*(z(1,ii)+h)/h)) ...
+((C8*cosh(8*pi*l/h) + D8*sinh(8*pi*l/h))*cos(8*pi*(z(1,ii)+h)/h))) - P(1,ii);
eqIII(ii) = (1/h)*(D0) + (2/h)*(((C1*(1*pi/h)*sinh(1*pi*l/h) + D1*(1*pi/h)*cosh(1*pi*l/h))*cos(1*pi*(z(1,ii)+h)/h)) ...
+ ((C2*(2*pi/h)*sinh(2*pi*l/h) + D2*(2*pi/h)*cosh(2*pi*l/h))*cos(2*pi*(z(1,ii)+h)/h)) ...
+ ((C3*(3*pi/h)*sinh(3*pi*l/h) + D3*(3*pi/h)*cosh(3*pi*l/h))*cos(3*pi*(z(1,ii)+h)/h)) ...
+ ((C4*(4*pi/h)*sinh(4*pi*l/h) + D4*(4*pi/h)*cosh(4*pi*l/h))*cos(4*pi*(z(1,ii)+h)/h)) ...
+ ((C5*(5*pi/h)*sinh(5*pi*l/h) + D5*(5*pi/h)*cosh(5*pi*l/h))*cos(5*pi*(z(1,ii)+h)/h)) ...
+ ((C6*(6*pi/h)*sinh(6*pi*l/h) + D6*(6*pi/h)*cosh(6*pi*l/h))*cos(6*pi*(z(1,ii)+h)/h)) ...
+ ((C7*(7*pi/h)*sinh(7*pi*l/h) + D7*(7*pi/h)*cosh(7*pi*l/h))*cos(7*pi*(z(1,ii)+h)/h)) ...
+ ((C8*(8*pi/h)*sinh(8*pi*l/h) + D8*(8*pi/h)*cosh(8*pi*l/h))*cos(8*pi*(z(1,ii)+h)/h)))- Q(1,ii);
end
eqI1 = eqI(1,1);
eqI2 = eqI(1,2);
eqI3 = eqI(1,3);
eqI4 = eqI(1,4);
eqI5 = eqI(1,5);
eqI6 = eqI(1,6);
eqII1 = eqII(1,1);
eqII2 = eqII(1,2);
eqII3 = eqII(1,3);
eqII4 = eqII(1,4);
eqII5 = eqII(1,5);
eqII6 = eqII(1,6);
eqIII1 = eqIII(1,1);
eqIII2 = eqIII(1,2);
eqIII3 = eqIII(1,3);
eqIII4 = eqIII(1,4);
eqIII5 = eqIII(1,5);
eqIII6 = eqIII(1,6);
result = vpasolve([eqI1,eqI2,eqI3,eqI4,eqI5,eqI6,eqII1,eqII2,eqII3,eqII4,eqII5,eqII6,eqIII1,eqIII2,eqIII3,eqIII4,eqIII5,eqIII6])
disp 'Cm = [C0 C1 C2 C3 C4 C5 C6 C7 C8]'
Cminitial = [result.C0 result.C1 result.C2 result.C3 result.C4 result.C5 result.C6 result.C7 result.C8];
Cm = double (Cminitial)
disp 'Dm = [D0 D1 D2 D3 D4 D5 D6 D7 D8]'
Dminitial = [result.D0 result.D1 result.D2 result.D3 result.D4 result.D5 result.D6 result.D7 result.D8];
Dm = double (Dminitial)

Connectez-vous pour commenter.

Réponses (1)

eqI = zeros (1,points, 'sym');
eqII = zeros (1,points, 'sym');
eqIII = zeros (1,points, 'sym');

4 commentaires

OMG!!!! haha Thank you
Any idea to optimize writing the serie...?
C3*(3*pi/h)*sinh(3*pi*l/h) + D3*(3*pi/h)*cosh(3*pi*l/h))*cos(3*pi*(z(1,ii)+h)/h)
suggests
vector C
vector D
N = 1:8;
Npih = N.*pi.*h
sum( C .* Npih .* sinh(Npih/l) + D .* Npih .* cosh(Niph./l) .* cos(Npih.*(z(1,ii)+h), 2)
except I would probably take it further and z(1,:) instead of z(1,ii) so that the for ii loop could be avoided.
Hi, I introduced your suggestion (with some changes responding to my equations). However, the final equations are diffferent and also...the solution for the equations system is empty
clear
clc
% General input
h = 8;
l = 2;
d = 0.09;
sections = 5;
% Data from previous calculations
z = [0,-1.6,-3.2,-4.8,-6.4,-8];
P = [-0.923072381390763 + 0.083645843312860i,-0.763379411108664 + 0.047842477328015i,-0.647844785539475 + 0.001886672095714i,-0.569785303973405 - 0.037689986193477i,-0.524685547937092 - 0.062861417563918i,-0.509936682892244 - 0.071256842113728i];
Q = [0.000000000000000 + 0.100765147467902i,0.000000000000000 + 0.093981142166511i,0.000000000000000 + 0.095329989814646i,0.000000000000000 + 0.100156105292950i,0.000000000000000 + 0.099088979609004i,0.000000000000000 + 0.096724494080166i];
points = sections + 1;
eqI = zeros (1,points, 'sym');
eqII = zeros (1,points, 'sym');
eqIII = zeros (1,points, 'sym');
syms C0 C1 C2 C3 C4 C5 C6 C7 C8 D0 D1 D2 D3 D4 D5 D6 D7 D8
C = [C1 C2 C3 C4 C5 C6 C7 C8];
D = [D1 D2 D3 D4 D5 D6 D7 D8];
N = 1:8;
Npih = N.*pi./h;
for ii = 1:points
eqI(ii) = (1/h)*C0 + (2/h)*sum( C .*cos(Npih.*(z(1,ii)+h)), 2)-d;
% eqI(ii) = (1/h)*C0 + (2/h)*(C1*cos(1*pi*(z(1,ii)+h)/h)+...+C8*cos(8*pi*(z(1,ii)+h)/h))-d;
eqII(ii) = (1/h)*(C0+D0*l) + (2/h)*sum( C .* Npih .* cosh(Npih.*l) + D .*sinh(Npih.*l).* cos(Npih.*(z(1,ii)+h)), 2) - P(1,ii);
% eqII(ii) = (1/h)*(C0+D0*l) + (2/h)*(((C1*cosh(1*pi*l/h) + D1*sinh(1*pi*l/h))*cos(1*pi*(z(1,ii)+h)/h))+...+((C8*cosh(8*pi*l/h) + D8*sinh(8*pi*l/h))*cos(8*pi*(z(1,ii)+h)/h))) - P(1,ii);
eqIII(ii) = (1/h)*(D0) + (2/h)*sum( C .* Npih .* sinh(Npih.*l) + D .* Npih .* cosh(Npih.*l) .* cos(Npih.*(z(1,ii)+h)), 2)- Q(1,ii);
% eqIII(ii) = (1/h)*(D0) + (2/h)*(((C1*(1*pi/h)*sinh(1*pi*l/h) + D1*(1*pi/h)*cosh(1*pi*l/h))*cos(1*pi*(z(1,ii)+h)/h))+...+ ((C8*(8*pi/h)*sinh(8*pi*l/h) + D8*(8*pi/h)*cosh(8*pi*l/h))*cos(8*pi*(z(1,ii)+h)/h)))- Q(1,ii);
end
eqI1 = eqI(1,1);
eqI2 = eqI(1,2);
eqI3 = eqI(1,3);
eqI4 = eqI(1,4);
eqI5 = eqI(1,5);
eqI6 = eqI(1,6);
eqII1 = eqII(1,1);
eqII2 = eqII(1,2);
eqII3 = eqII(1,3);
eqII4 = eqII(1,4);
eqII5 = eqII(1,5);
eqII6 = eqII(1,6);
eqIII1 = eqIII(1,1);
eqIII2 = eqIII(1,2);
eqIII3 = eqIII(1,3);
eqIII4 = eqIII(1,4);
eqIII5 = eqIII(1,5);
eqIII6 = eqIII(1,6);
result = vpasolve([eqI1,eqI2,eqI3,eqI4,eqI5,eqI6,eqII1,eqII2,eqII3,eqII4,eqII5,eqII6,eqIII1,eqIII2,eqIII3,eqIII4,eqIII5,eqIII6]);
disp 'Cm = [C0 C1 C2 C3 C4 C5 C6 C7 C8]'
Cminitial = [result.C0 result.C1 result.C2 result.C3 result.C4 result.C5 result.C6 result.C7 result.C8];
Cm = double (Cminitial)
disp 'Dm = [D0 D1 D2 D3 D4 D5 D6 D7 D8]'
Dminitial = [result.D0 result.D1 result.D2 result.D3 result.D4 result.D5 result.D6 result.D7 result.D8];
Dm = double (Dminitial)
As the question is not about how to write the algorithm at all, and is instead about how to write the algorithm "more nicely", then you can
eqI(ii) = (1/h)*C0 + (2/h)*sum( C .*cos(Npih.*(z(1,ii)+h)), 2)-d;
eqI_old(ii) = (1/h)*C0 + (2/h)*(C1*cos(1*pi*(z(1,ii)+h)/h)+...+C8*cos(8*pi*(z(1,ii)+h)/h))-d; %all of the old code
and compare compare the two, such as checking whether their difference is 0. If the two are different, then start debugging.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by