필터 지우기
필터 지우기

What does this code say? (Linear Algebra related) (Poisson)

조회 수: 2 (최근 30일)
Guile
Guile 2013년 10월 29일
답변: sixwwwwww 2013년 10월 29일
This is my first time using MATLab and I have no idea what this code is saying. Can anyone translate this for me? I have proficient skills in Java, but it's not helping me out here.
p = 10; n = 2.^(1:p)'; % grid sizes
err = zeros(p,1); % error for each n
for i = 1:p
h = 1/n(i); x = (1:n(i)-1)'*h;
A = (1/h^2)*(diag(2*ones(n(i)-1,1))-diag(ones(n(i)-2,1), 1) ...
-diag(ones(n(i)-2,1),-1));
f = exp(x); % right-hand side
v = A\f; % solve
u = -exp(x)+(exp(1)-1)*x+1; % exact soln
err(i) = max(abs(u-v)); % infinity-norm error
end
format short e
disp(' ')
disp(' h err err/h^2 ')
disp(' ----- ------- -----------')
disp(' ')
disp([1./n err err./(1./n.^2)])
I need a step by step explanation, if possible. For example, I have no idea what's going on with n on the first line. What does 2.^(1:p)' do? Like the period, the carrot, the parentheses, the colon, specifically? When it runs, it looks like this:
h err err/h^2
----- ------- -----------
5.0000e-01 4.3295e-03 1.7318e-02
2.5000e-01 1.0925e-03 1.7480e-02
1.2500e-01 2.7377e-04 1.7521e-02
6.2500e-02 6.8827e-05 1.7620e-02
3.1250e-02 1.7234e-05 1.7647e-02
1.5625e-02 4.3098e-06 1.7653e-02
7.8125e-03 1.0776e-06 1.7655e-02
3.9063e-03 2.6940e-07 1.7655e-02
1.9531e-03 6.7351e-08 1.7656e-02
9.7656e-04 1.6838e-08 1.7656e-02

답변 (1개)

sixwwwwww
sixwwwwww 2013년 10월 29일
Dear Guile, I placed references for all the things which maybe confusing for you. You can try to access those references to find out how the code works:
p = 10;
% References for below code line:
% for (:) http://www.mathworks.de/de/help/matlab/ref/colon.html
% for (') http://www.mathworks.de/de/help/matlab/ref/transpose.html
% for (.^) http://www.mathworks.de/de/help/matlab/ref/power.html
n = 2.^(1:p)';
% References for below code line:
% for (zeros) http://www.mathworks.de/de/help/matlab/ref/zeros.html
err = zeros(p,1); % error for each n
% References for below code line:
% for (for) http://www.mathworks.de/de/help/matlab/ref/for.html
for i = 1:p
h = 1 / n(i);
x = (1:n(i) - 1)' * h;
% References for below code line:
% for (diag) http://www.mathworks.de/de/help/matlab/ref/diag.html
% for (ones) http://www.mathworks.de/de/help/matlab/ref/ones.html
A = (1 / h^2) * (diag(2*ones(n(i)-1,1))-diag(ones(n(i)-2,1), 1)-diag(ones(n(i)-2,1),-1));
% References for below code line:
% for (exp) http://www.mathworks.de/de/help/matlab/ref/exp.html
f = exp(x);
v = A\f;
u = -exp(x)+(exp(1)-1)*x+1;
% References for below code line:
% for (max) http://www.mathworks.de/de/help/fixedpoint/ref/max.html
err(i) = max(abs(u-v));
end
% References for below code line:
% for (format) http://www.mathworks.de/de/help/matlab/ref/format.html
format short e
% References for below code line:
% for (disp) http://www.mathworks.de/de/help/matlab/ref/disp.html
disp(' ')
disp(' h err err/h^2 ')
disp(' ----- ------- -----------')
disp(' ')
% References for below code line:
% for (./) http://www.mathworks.de/de/help/matlab/ref/rdivide.html
disp([1./n err err./(1./n.^2)])
I hope it can help you somehow. Good luck!

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by