How can I constrcut this matrix for a problem?

 Accepted Answer

If x = [x0,x1,...,xn] is an 1x(n+1) row vector, you could use
M = zeros(n+1);
for i = 1:n+1
M(i,:) = psi(x(1:n+1)-x(i));
end
M = M - diag(diag(M)) + eye(n+1)
if psi is a function that accepts array input.

4 Comments

Hello, thank you for your time, when I try your code for a vector x = [1 2 3 4 5], I get this error.
x = 1:5;
n = numel(x)-1;
psi = @(x)exp(-3*x.^2);
M = zeros(n+1);
for i = 1:n+1
M(i,:) = psi(x(1:n+1)-x(i));
end
M = M - diag(diag(M)) + eye(n+1);
format long
M
M = 5×5
1.000000000000000 0.049787068367864 0.000006144212353 0.000000000001880 0.000000000000000 0.049787068367864 1.000000000000000 0.049787068367864 0.000006144212353 0.000000000001880 0.000006144212353 0.049787068367864 1.000000000000000 0.049787068367864 0.000006144212353 0.000000000001880 0.000006144212353 0.049787068367864 1.000000000000000 0.049787068367864 0.000000000000000 0.000000000001880 0.000006144212353 0.049787068367864 1.000000000000000
Amazing, one last thing that I dont get, how can I make this code into a function so that I can put any input vector x and the output would be the conversion number of the matrix M?
Save "untitled245.m" as "fun.m" in your working directory and try again.
Because your function psi satisfies psi(0)=1, you can remove the line
M = M - diag(diag(M)) + eye(n+1);
in the code.
If you want to use the code for other functions, too, that might not satisfy psi(0)=1, better keep it.
Maybe besides x, you should pass "psi" to "fun" as a function handle to keep the code more flexible.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!