Inserting a function in a matrix.

조회 수: 22 (최근 30일)
Jason Psimopoulos
Jason Psimopoulos 2020년 12월 7일
편집: Jason Psimopoulos 2020년 12월 9일
I am trying to solve the system above.I think I got right the first part of the quation with a for loop(code bellow)
L = 51;
ws1 = 0.4*pi;
wp1 = 0.5*pi;
Ks1 = 1;
Kp = 2;
wo = (ws1+wp1)/2;
G = 1000;
w = [0:(G)]*pi/G;
W = Ks1*(w<=ws1) + Kp*(w>=wp1)
D = (wo>=w);
N=(L-1)/2
m=0:N
k=1:1001
for i = 0:N
F(i+1,:)=sum(arrayfun(@(k) (W(k).^2).*D(k).*cos(w(k).*i), 1:1000))
end
I am struggling though in the second part.My code looks like this but it does not seem to work.
for i = 0:N
for p = 0:N
F2=@(k)(W(k).^2).*D(k).*(cos(w(k).*i).*(cos(w(k).*p))')
end M[i+1,p+1]=sum[arrayfun(@(k) F2(k), 1:1000)]
end
Does anyone know how I could possibly fix that.I also download the symbolic math toolbox but I was not able to find something to help me with that.
  댓글 수: 2
Théophane Dimier
Théophane Dimier 2020년 12월 8일
편집: Théophane Dimier 2020년 12월 8일
Hi,
that line looks a bit weird:
end M[i+1,p+1]=sum[arrayfun(@(k) F2(k), 1:1000)]
What do you want to do there? you should not have a statement after the end. Also, functions are called with parenthese () and not brackets []. To index arrays one should also use parentheses () and not brackets [] (unlike python or C)
Considering that it was a typo, would the following work?
M(i+1,p+1)=sum(arrayfun(@(k) F2(k), 1:1000))
end
Jason Psimopoulos
Jason Psimopoulos 2020년 12월 9일
Hi,
Thanks for taking the time with my question.Frankly my whole code looked weird.I had many scripts with different tests.I finally found the solution though.I will put it here in case someone needs something simillar in the future.
for i=1:length(OM) %OM is the wk samples vector(grid size 5000 in my case)
COS=cos( [0:N]' * OM(i) );
Q=Q+(W(i)^2)*COS*(COS');
P=P+(W(i)^2)*D(i)*COS;
end
A=Q\P;
A(2:end)=A(2:end)/2;

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

채택된 답변

Jason Psimopoulos
Jason Psimopoulos 2020년 12월 9일
편집: Jason Psimopoulos 2020년 12월 9일
for i=1:length(OM) %OM is the wk samples vector(grid size 5000 in my case)
COS=cos( [0:N]' * OM(i) );
Q=Q+(W(i)^2)*COS*(COS');
P=P+(W(i)^2)*D(i)*COS;
end
A=Q\P;
A(2:end)=A(2:end)/2;
That is the code I ended up with in case someone needs something similar.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by