I'm getting a matrix as output when I'm expecting a vector

3 次查看(过去 30 天)
I'm trying to calculate the abscisses and weight values for the Newton-Cotes method. In the end I use a linsolve() expecting to get a vector as output but instead I get a matrix. Any ideas what I could have done wrong?
a = 0;
b = 1;
n = 20;
[x,H] = NewtonCotes(a,b,n)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 5.742547e-17.
x = 1x21
0 0.0500 0.1000 0.1500 0.2000 0.2500 0.3000 0.3500 0.4000 0.4500 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 0.9500 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
H = 21x21
1.0e+12 * 7.0832 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 -0.9026 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 5.6187 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 -0.7159 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 3.0530 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 -0.3890 0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0612 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 -0.0078 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 -2.5218 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.3213 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function [x,H] = NewtonCotes(a,b,n)
if n ~= 0
h = (b-a)/n;
else
h = (b-a);
end
for i = 1:n+1
x(i) = a + ((i-1)*h);
end
H = ComputeWeights(a,b,x);
end
function [H] = ComputeWeights(a,b,x)
n = length(x)-1;
I = ones(n+1);
I(1) = b - a;
I(2) = 0;
for i = 3:n+1
I(i) = ((b-a)*((-1)^i + 1))/(2*(1-i^2));
end
for i = 1:length(x)
x(i) = ((2/(b-a))*(x(i)-a))-1;
end
T = zeros(n+1,n+1);
for i = 1:n+1
for k = 1:n+1
T(i,k) = cos(k*acos(x(i)));
end
end
H = linsolve(T,I);
end
  2 个评论
Rahul
Rahul 2024-8-6
What inputs are you passing to your function?
I have tried to use some dummy inputs using your function and it was returning a vector instead of a matrix.
Torsten
Torsten 2024-8-6
I is an (n+1)x(n+1) matrix in the code. So H will always be a (n+1)x(n+1) matrix, not a vector.

请先登录,再进行评论。

回答(2 个)

Torsten
Torsten 2024-8-6
Maybe you mean
I = ones(n+1,1);
instead of
I = ones(n+1);

Shivansh
Shivansh 2024-8-6
编辑:Shivansh 2024-8-6
Hello Arthur,
You can get a matrix as output while using linsolve to solve the linear systems of equation.
The function returns a vector or matrix that satisfies AX=B. The size of X depends on whether "opts.TRANSA" = true:
  • If A is m-by-n and B is m-by-k, then X is n-by-k and is the solution to AX = B.
  • If "opts.TRANSA" = true, then A is m-by-n and B is m-by-k. In this case, X is m-by-k and is the solution to A'X = B.
Here, "TRANSA" is Conjugate transpose which specifies whether the function solves A*X = B (when opts.TRANSA = false) or the transposed problem A'*X = B (when opts.TRANSA = true).
Please refer to the following documentation link for more information:
I hope it resolves your query.

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by