Create matrix from columns of variable (a)-for loop

조회 수: 2 (최근 30일)
Wazy sky
Wazy sky 2018년 9월 26일
답변: Wazy sky 2018년 10월 13일
Hi I want to create a matrix TA from the columns EE generated in the last loop. I can display the wanted columns "disp (EE)' but when adding the matrix want to create from these columns, an error occurred, noted that the vectors/columns EE has a variable (a)
You may ignore the lines of the code before the line start with C = [I;A;B]; for getting the question
I=ones(20,2);
for i=0:20
theta=(pi/40)*i;
r=2.5;x1=r*cos(theta);
y1=r*sin(theta);s=i+1;
I(s,:)=[x1,y1];
end
A=ones(20,2);
for k=1:20
r=(k-1)/10;
A(k,:)=[1,r];
end
m=1:-(1/47):0;
B=ones(length(m),2);
B(:,1)=m';B(:,2)=2;
C = [I;A;B];
%%find the complex variable matrix, ZC connjgate Z
Z=ones(length(C),1);
for v=1:length(C)
Z(v)=C(v,1)+1i*C(v,2);end
ZC=conj(Z);
F=ones(89,2);syms Fb;
for ii=1:89
if ii<22
F(ii,1)=0;F(ii,2)=0;
elseif ii<42
F(ii,1)=1; F(ii,2)=0;
else
F(ii,1)=(89-ii)/(89-42);F(ii,2)=0;
end
end
FF=F.*Fb;
zzc=Z-ZC;
QQ=ones(length(Z),21);SS=ones(length(Z),21);ss=0;
T1=ones(length(Z),21);T2=ones(length(Z),21);syms a
for K=-6:14
ss=ss+1;
QQ(:,ss)=Z.^(2*K-1)+ZC.^(2*K-1);
SS(:,ss)=(2*ss-1)*(Z.^(2*ss-2));
T1(:,ss)=Z.^(2*K-2);T2(:,ss)=ZC.^(2*K-2);
end
%multiplying the T1,T2 BY THE FCTOR
Q=QQ.';S=SS.';
TA=ones(89,21);TB=ones(89,21);
Tz=sqrt(Z.^2-a^2); Tzc=sqrt(ZC.^2-a^2);
for j=1:21
CC=Tz.*T1(:,j);DD=Tzc.*T2(:,j);EE=CC+DD;
TA(:,j)=EE;
end
  댓글 수: 2
KSSV
KSSV 2018년 9월 26일
You must specify the error and error line number.
Wazy sky
Wazy sky 2018년 9월 26일
Sorry missed that
Error The following error occurred converting from sym to double: DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use VPA.
Error in filename (line 67)
TA(:,j)=EE;

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 9월 26일
syms a
so a is syms
Tz=sqrt(Z.^2-a^2); Tzc=sqrt(ZC.^2-a^2);
so Tz and Tzc are syms involving the unresolved variable a
CC=Tz.*T1(:,j);DD=Tzc.*T2(:,j);EE=CC+DD;
so CC and DD and EE all involve that unresolved variable a
TA=ones(89,21);
so TA can only store numeric values.
TA(:,j)=EE;
with TA only being able to store numeric values, the values in EE have to be converted from sym to numeric. But they cannot be converted, because they involve the unresolved symbolic variable a
Your EE entries are formulas, such as
(5209627982755595*(7990063086789097/36028797018963968 - a^2)^(1/2))/137438953472 + (5252908098854583*(6546788930781455/18014398509481984 - a^2)^(1/2))/4398046511104
You cannot store a formula in a numeric array.
You can use vpa() to convert the formulas from using rational numbers to using floating point numbers; for example vpa() of the above gives the result
1194.3730212020871022104984149337*(0.36341979041573407860354905096756 - 1.0*a^2)^(1/2) + 37905.032388193612860050052404404*(0.22176880017902014796682408359629 - 1.0*a^2)^(1/2)
but this is still a formula and would need a symbolic array to be stored into.
  댓글 수: 16
Stephen23
Stephen23 2018년 10월 11일
"Your suggestions are so much appreacited"
Your code is very badly aligned. Badly aligned code is one way that beginners hide bugs in their code. In this case, your badly aligned code has hidden several bugs and made them harder to identify. Correctly aligned code makes identifying some kinds of bugs easier. You should align your code correctly.
I recommend using the MATLAB Editor's default alignment settings, which occur automatically as you type. You can also align existing code: select the code text, press ctrl+i.
Wazy sky
Wazy sky 2018년 10월 11일
Thanks, Stephen Cobeldick; I tried to simplify the question by presenting only the main part of the code, editing in text file led to losing its alignment. I am posting all the code here
use *a=0.5, beta=45* for example, please ignoreor texted part. Thanks!
prompt={'a:','beta:'};
% Create all your text fields with the questions specified by the variable prompt.
title='Input';
% The main title of your input dialog interface.
answer=inputdlg(prompt,title);
a = str2double(answer{1});
beta = str2double(answer{2});
circle_pts=zeros(37,2);
tic
for j=0:36
theta=(pi/36)*j;
r=0.25;
x1=r*cos(theta);
y1=r*sin(theta);
circle_pts(j+1,:)=[x1,y1];
end
% Right-Side RS;x0=b/cos(pi/2-beta);x0=b/cos(pi/2-beta);b=1;
b=1;
x0=b/cos(deg2rad(90-beta));
h=2; RS=h-b*tan(deg2rad(90-beta));
de_RS=RS/20;RS_pts=zeros(20,2);
for jj=0:19
x_RS=x0+jj*de_RS*cos(deg2rad(beta));
y_RS=jj*de_RS*sin(deg2rad(beta));
RS_pts(jj+1,:)=[x_RS,y_RS];
end
% Top-side TS_pts 58-78,23 De=2b/22;x1=RS*cos(90-beta)+xO, y1=RS*sin(beata)
TS=2; % TS the top side length
De_TS=2/20;TS_pts=zeros(21,2);% corner point frpm RS included.
for k=0:20
X_TS=x0+RS*cos(deg2rad(beta))-k*De_TS*cos(deg2rad(90-beta));
Y_TS=RS*sin(deg2rad(beta))+k*De_TS*sin(deg2rad(90-beta));
TS_pts(k+1,:)=[X_TS,Y_TS];
end
% left side LS=H-RS, h=.5*H;h=2; Angle=beta,
LS=2*(h)-RS;
LS_pts=zeros(20,2);
De_LS=LS/20;
for kk=1:20
X_LS=TS_pts(21,1)-kk*De_LS*cos(deg2rad(beta));
Y_LS=TS_pts(21,2)-kk*De_LS*sin(deg2rad(beta));
LS_pts(kk,:)=[X_LS, Y_LS];
end
% geometry coordinates
pt_1=[x0,0];
pt_2=[x0+RS*cos(deg2rad(beta)), RS*sin(deg2rad(beta))];
pt_3=[pt_2(1,1)-TS*cos(deg2rad(90-beta)), pt_2(1,2)+TS*sin(deg2rad(90-beta))];
pt_4=[pt_3(1,1)-LS*cos(deg2rad(beta)),pt_3(1,2)-LS*sin(deg2rad(beta))];
% the coordinate matrix [C]
C=[circle_pts;RS_pts;TS_pts;LS_pts]; t_coordinat=toc;
Z=ones(length(C),1);
for v=1:length(C)
Z(v)=C(v,1)+1i*C(v,2);
end
ZC=conj(Z);
Z_ZC=Z-ZC;
% Loading matrix. assuming the load accumlating.
% for m=1:98
% if m<38
% F(m,1)=0;F(m,2)=0+1i*0;
% elseif m<57
% F(m,1)=-((m-38)/20)*RS*cos(deg2rad(beta)); F(m,2)=0+1i*((m-38)/20)*RS*sin(deg2rad(beta));
% elseif m==58
% F(m,1)=-((m-38)/20)*RS*cos(deg2rad(beta))+1*sin(deg2rad(beta)); F(m,2)=0+1i*(((m-38)/20)*RS*sin(deg2rad(beta))+cos(deg2rad(beta)));
% elseif m<78
% F(m,1)=((68-m)/(68-58))*sin(deg2rad(beta));F(m,2)=0+1i*((68-m)/(68-58))*cos(deg2rad(beta));
% else
% F(m,1)=((m-78)/(89-78))*LS*cos(deg2rad(beta));F(m,2)=0-1i*((m-78)/(89-78))*LS*sin(deg2rad(beta));
% end
% end
% FF=F.*Fb;
% the (-) in fy should be kept as fy calculated here for +Y
%CHECK THAT FOR B=45 A ZERO FOR PT78; PT58.
F=ones(98,2);Fb=1;
for n=1:98
if n<38
F(n,1)=0;F(n,2)=0+1i*0;
elseif n<57
F(n,1)=-cos(deg2rad(beta)); F(n,2)=0+1i*sin(deg2rad(beta));
elseif n==58
F(n,1)=0.0*(-cos(deg2rad(beta))+1*sin(deg2rad(beta))); F(n,2)=0+1i*(sin(deg2rad(beta))+cos(deg2rad(beta)));
elseif n<78
F(n,1)=((68-n)/(68-58))*sin(deg2rad(beta));F(n,2)=0+1i*((68-n)/(68-58))*cos(deg2rad(beta));
elseif n==78
F(n,1)=0*cos(deg2rad(beta))-1*sin(deg2rad(beta));F(n,2)=-(0+1i*(sin(deg2rad(beta))+cos(deg2rad(beta))));
else
F(n,1)=cos(deg2rad(beta));F(n,2)=0-1i*sin(deg2rad(beta));
end
end
% --->>> CORRECTION THE RS direction<<------
% for n=1:98
% if n<38
% F(n,1)=0;F(n,2)=0+1i*0;
% elseif n<57
% F(n,1)=-cos(deg2rad(beta)); F(n,2)=0+1i*sin(deg2rad(beta));
% elseif n==58
% F(n,1)=0.0*(-cos(deg2rad(beta))+1*sin(deg2rad(beta))); F(n,2)=0+1i*(sin(deg2rad(beta))+cos(deg2rad(beta)));
% elseif n<78
% F(n,1)=((68-n)/(68-58))*sin(deg2rad(beta));F(n,2)=0+1i*((68-n)/(68-58))*cos(deg2rad(beta));
% elseif n==78
% F(n,1)=cos(deg2rad(beta))+1*sin(deg2rad(beta));F(n,2)=(0+1i*(sin(deg2rad(beta))-cos(deg2rad(beta))));
% else
% F(n,1)=-cos(deg2rad(beta));F(n,2)=0+1i*sin(deg2rad(beta));
% end
% end
F(:,1)=F(:,1)*(-1);
FF=F.*Fb;
QQ=ones(length(Z),29);SS=ones(length(Z),29);
T1=ones(length(Z),29);T2=ones(length(Z),29);
RR=ones(length(Z),29);
ss=0; tic
for K=-10:18
ss=ss+1;
QQ(:,ss)=Z.^(2*K-1)-ZC.^(2*K-1);
SS(:,ss)=(2*K-1)*(Z.^(2*K-2));
RR(:,ss)=((2*K-1)-((2*K-2).*(a./Z).^2)).*Z.^(2*K-1);
T1(:,ss)=Z.^(2*K-2);T2(:,ss)=ZC.^(2*K-2);
end
P=ones(98,29);t_QSRT=toc;
Tz=((Z.^2-a^2).^0.5); Tzc=((ZC.^2-a^2).^0.5);
for j=1:29
CC=(Tz.*T1(:,j));DD=(Tzc.*T2(:,j));EE=(CC+DD);
P(:,j)=(EE);
end
Tzz=1./Tz;
E=sym('E',[29 1]);G=sym('G',[29 1]);
tic;
H1=P*E;
H2=QQ*G;
H3=SS*G;
H4=RR*E;
H5=Tzz.*H4;
H6=H5+H3;
H7=conj(H6);
H8=Z_ZC.*H7;
H=H1+H2+H8;
H_Real= real(H);
%has Real&imag parts for both variables.
H_imag = imag(H);
t_H=toc; tic;
Eq1=(H_Real-FF(:,1)).^2+(H_imag-FF(:,2)).^2;t_Eq1=toc;
EmE=sym(ones(58,1));n=0;tic;Pluss=0;Minuss=0;
% the inflection point diff(diff()) checked all points are Min
for ij=1:29
n=n+1;
nn=0;
Em1=0;Em2=0;
for jj=1:98
Em1=Em1+diff(Eq1(jj),E(ij,1));
Em2=Em2+diff(Eq1(jj),G(ij,1));nn=nn+1;
end
if (diff((Em1),E(ij,1))>=0)
Pluss=Pluss+1;
else Minuss=Minuss+1;
end
EmE(ij,1)=Em1;EmE(ij+29,1)=Em2;
end
t_EmE=toc;
tic;
sol=vpasolve(EmE,[E;G]);
t_sol=toc; ASol= real(sol.E1)
sol.E1;sol.E2;sol.E3;sol.E4;sol.E5;sol.E6;sol.E7;sol.E8;sol.E9;sol.E10;sol.E11;sol.E12;sol.E13;sol.E14;sol.E15;sol.E16;sol.E17;sol.E18;sol.E19;sol.E20;sol.E21;sol.E22;sol.E23;sol.E24;sol.E25;sol.E26;sol.E27;sol.E28;sol.E29;
FE=(2*sol.E1*(a)^(-22)+2*sol.E2*(a)^(-20)+2*sol.E3*(a)^(-18)+2*sol.E4*(a)^(-16)+2*sol.E5*(a)^(-14)+2*sol.E6*(a)^(-12)+2*sol.E7*(a)^(-10)+2*sol.E8*(a)^(-8)+2*sol.E9*(a)^(-6)+2*sol.E10*(a)^(-4)+2*sol.E11*(a)^(-2)+2*sol.E12*(a)^(0)+2*sol.E13*(a)^(2)+2*sol.E14*(a)^(4)+2*sol.E15*(a)^(6)+2*sol.E16*(a)^(8)+2*sol.E17*(a)^(10)+2*sol.E18*(a)^(12)+2*sol.E19*(a)^(14)+2*sol.E20*(a)^(16)+2*sol.E21*(a)^(18)+2*sol.E22*(a)^(20)+2*sol.E23*(a)^(22)+2*sol.E24*(a)^(24)+2*sol.E25*(a)^(26)+2*sol.E26*(a)^(28)+2*sol.E27*(a)^(30)+2*sol.E28*(a)^(32)+2*sol.E29*(a)^(34));

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

추가 답변 (2개)

Wazy sky
Wazy sky 2018년 10월 11일
편집: Walter Roberson 2018년 10월 11일
Hi Walter Roberson;
Thank you for your comment
Regards 1st comment:
I'm so sorry as I cleaned my code from extra lines, some left behind. should not be a problem there based on your comment & the original one below:
Regards 2nd comment:
Sorry again for that tic-toc, & n,nn.I should delete all them from my code when I was to post it. appears missed some of them. They are not part of the problem. I used them to check the timing tic-toc. For (nn) I used to check that the loop works well.
Pluss=0; Minuss=0; % used these to test/check that the points (Em1, Em2) are the minimum and they are all mimunm, as the 2nd diff >=0;
n=0;
for ij=1:29
n=n+1;
nn=0;
Em1=0;Em2=0;
for jj=1:98
Em1=Em1+diff(Eq1(jj),E(ij,1));
Em2=Em2+diff(Eq1(jj),G(ij,1));nn=nn+1;
end
if (diff((Em1),E(ij,1))>=0)
Pluss=Pluss+1;
else Minuss=Minuss+1;
end
EmE(ij,1)=Em1;EmE(ij+29,1)=Em2;
end
The issue with code is the warning for the explicit solution.
Thanks your comment is appreciated
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 10월 11일
If you dbstop at the vpasolve() and examine vpa(EmE) then you will see that it has a large number of real constants times real() or imag() of various variables, added together, with no obvious cross-products or exponentiation or exp() or the like. So each row is linear in real() and imag() of those variables. But each row also ends in a constant that has both a real and an imaginary coefficient. With all of those constants being real valued, and with real() and imag() also returning real-valued information, then the part involving the variables must be real-valued. That means there is no possibility of part of a term canceling out the imaginary part of the constant coefficient of the row. Therefore the rows cannot equal 0, because although you might balance the real portion, you are left with a non-zero imaginary portion.
Wazy sky
Wazy sky 2018년 10월 12일
Walter Roberson, Thank you for highlighting this point. I feel I have to check back the constructed matrices in case something misrepresented. Your point is very important to me and helpful to know at least that as I will not getting the expected results (complex number) there is a mistake here.
However, please bear with me to double check is that mean if the constant at the end of the equation was only real, then for this form of the equation, I will have a solution of a complex number
AS from my first practice with the code where I know the variables are real constant. the solution was real as expected. checking the equations there, they have the same form that you mentioned here [constant*real+/- another-constant*imag +/-another-constant with the only real part. does that mean there is something missing there, or it is still possible to get a real solution for all the variables? Thanks

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


Wazy sky
Wazy sky 2018년 10월 13일
Well I found where is the problem in the code?1
So when I extracted the real and imaginary part from my equations
H_imag = imag(H);
H_Real= real(H);
It seems that Matlab does not do it the right way, or as we may expect at this line when we run the code still see a mix of real & imaginary parts in both the H_imag, & H_Real, and here the problem
so the challenge is how to separate these parts. does Matlab, Mathematica or other software can solve the issue.
I found a source transform the same equation I have from the complex number in cartesian to polar coordinate. This step seems helped to separate the real and imaginary parts successfully
Please see the attached
I tried to convert the complex numbers to polar in MATLAB and separate the Real & imaginary parts but this did not work.
Thanks for your time and help in advance

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by