why does the following code give error?

u=[3 4 30 50];% Desired Vector
b=u;
[R,C]=size(b);
P=C/2;
M=2*C;
% calculate observed vector
xo=zeros(1,M);
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+1*exp(1i*((-pi/2)*sin(u(P+i))*(k-1)+(pi/(16*u(i)))*cos^2(u(P+i))*(k-1)^2)));
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
end
end
xe=zeros(1,M);
for k=1:M
for i=1:P
xe(1,k)=xe(1,k)+1*exp(1i*((k-1)*(-pi/2)*sin(b(P+i))+(pi/(16*b(i)))*cos^2(b(P+i))*(k-1)^2));
end
end
abc=0.0;
for m1=1:M
abc=abc+(abs(xo(1,m1)-xe(1,m1))).^2;
end
abc=abc/M;
e=abc

 採用された回答

Torsten
Torsten 2023 年 12 月 13 日

0 投票

Use
xo(1,k)=xo(1,k)+1*exp(1i*((-pi/2)*sin(u(P+i))*(k-1)+(pi/(16*u(i)))*cos(u(P+i))^2*(k-1)^2));
xe(1,k)=xe(1,k)+1*exp(1i*((k-1)*(-pi/2)*sin(b(P+i))+(pi/(16*b(i)))*cos(b(P+i))^2*(k-1)^2));
instead of
xo(1,k)=xo(1,k)+1*exp(1i*((-pi/2)*sin(u(P+i))*(k-1)+(pi/(16*u(i)))*cos^2(u(P+i))*(k-1)^2)));
xe(1,k)=xe(1,k)+1*exp(1i*((k-1)*(-pi/2)*sin(b(P+i))+(pi/(16*b(i)))*cos^2(b(P+i))*(k-1)^2));

5 件のコメント

Sadiq
Sadiq 2023 年 12 月 13 日
Thanks a lot dear Torsten fro your help. Yes now it works. But now it must give zero error "e" but it's not zero. Instead it gives "e=2.6077e-31". Why it is so?
Torsten
Torsten 2023 年 12 月 13 日
The accuracy of floating point arithmetic is limited:
Sadiq
Sadiq 2023 年 12 月 13 日
Thanks a lot dear Torsten for your prompt response. If it is so, then why does the following code gives zero er
ror?
clear all;clc
u=[3 4 30 50];% Desired vector
b=u;
[R,C]=size(b);
P=C/2;
M=2*C;
% calculate observed vector
xo=zeros(1,M);
for k=1:M
for i=1:P
xo(1,k)=xo(1,k)+u(i)*exp(-1i*(k-1)*pi*cos(u(P+i)));
end
end
xe=zeros(1,M);
for k=1:M
for i=1:P
xe(1,k)=xe(1,k)+b(i)*exp(-1i*(k-1)*pi*cos(b(P+i)));
end
end
abc=0.0;
for m1=1:M
abc=abc+(abs(xo(1,m1)-xe(1,m1))).^2;
end
abc=abc/M;
e=abc
Torsten
Torsten 2023 年 12 月 13 日
編集済み: Torsten 2023 年 12 月 13 日
Because even the order in which terms are multiplied can matter for the result.
Here, the terms that are summed are absolutly identical while in your previous code, the multiplicative order in both terms differs.
Sadiq
Sadiq 2023 年 12 月 13 日
Thanks a lot dear Torsten for your prompt response. Yes now I got it. Thank you once again.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2023 年 12 月 13 日

コメント済み:

2023 年 12 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by