forming a matrix with given entries

조회 수: 1 (최근 30일)
MADHVI
MADHVI 2023년 11월 4일
댓글: MADHVI 2023년 12월 20일
correct the use of if condition in the following code:
function [A,B] = fdm1(H,xi,a,b,N)
H = 1; xi = 0.1;
a= -1; b = 1; N=5;
h = (b-a)/N; h2 = h*h;
A = zeros(20*(N-1),20*(N-1));
B = zeros(20*(N-1),20*(N-1));
K = 10;
for k = 1:K;
A(2*(k-1)*(N-1)+1,2*(k-1)*(N-1)+1:2*(k-1)*(N-1)+2) = [-(2+(k*pi*h/H)^2) 1];
A(2*(k-1)*(N-1)+N,2*(k-1)*(N-1)+N:2*(k-1)*(N-1)+N+1) = [-(2+(k*pi*h/H)^2) 1];
A(2*(k-1)*(N-1)+N,2*(k-1)*(N-1)+2) = [h/2]
for n = 1:K;
if (n~=k) && any(n+k == 1:2:K) %ismember(n+k,1:2:K)
x = sum((2 * H * h * n * k * ((-1)^(n + k) - 1)) / ((n^2 - k^2)^2 * pi^2 * xi));
A(2*(k-1)*(N-1)+N,2*(n-1)*(N-1)+2) = [x]; end
B(2*(k-1)*(N-1)+1,2*(k-1)*(N-1)+2) = [h/2];
end
end
Thanks in advance.
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 11월 4일
편집: Dyuman Joshi 2023년 11월 4일
"correct the use of if condition in the following code:"
How would we know what is the correct use?
You will need to specify more than just simply showing the code.
What is the objective here? What is supposed to be the "correct" use?
What is the expected output?
MADHVI
MADHVI 2023년 11월 4일
Thank you for the response.
The objective is attached in the file.

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

답변 (1개)

prabhat kumar sharma
prabhat kumar sharma 2023년 12월 20일
Hi Madhvi,
I assume that you are trying to construct matrices `A` and `B` for a finite difference method (FDM) and are having trouble with the summation part of the second equation within the `if` condition. The code provided seems to have a few issues that need to be corrected, including the placement of the `if` block and the summation logic.
1. The `if` block should be properly nested within the `for` loops.
2. The `sum` function is not being used correctly. If you're trying to compute a sum based on the condition, you should accumulate the sum manually within the loop.
3.It's not clear what the indices for `A` and `B` should be in the summation part, but I'll assume you're trying to update the `(k,n)` element based on the condition.
function [A,B] = fdm1(H,xi,a,b,N)
% H = 1; xi = 0.1;
% a= -1; b = 1; N=5;
h = (b-a)/N; h2 = h*h;
A = zeros(20*(N-1),20*(N-1));
B = zeros(20*(N-1),20*(N-1));
K = 10;
for k = 1:K
A(2*(k-1)*(N-1)+1,2*(k-1)*(N-1)+1:2*(k-1)*(N-1)+2) = [-(2+(k*pi*h/H)^2) 1];
A(2*(k-1)*(N-1)+N,2*(k-1)*(N-1)+N:2*(k-1)*(N-1)+N+1) = [-(2+(k*pi*h/H)^2) 1];
A(2*(k-1)*(N-1)+N,2*(k-1)*(N-1)+2) = h/2; % Removed brackets for scalar assignment
B(2*(k-1)*(N-1)+1,2*(k-1)*(N-1)+2) = h/2; % Moved outside of the inner loop
for n = 1:K
if (n ~= k) && any(n+k == 1:2:K) % ismember(n+k,1:2:K)
x = (2 * H * h * n * k * ((-1)^(n + k) - 1)) / ((n^2 - k^2)^2 * pi^2 * xi);
A(2*(k-1)*(N-1)+N,2*(n-1)*(N-1)+2) = x; % Corrected the assignment
end
end
end
end
I hope it helps!
  댓글 수: 1
MADHVI
MADHVI 2023년 12월 20일
Thanks for your help. This issue has been resolved.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by