Borrar filtros
Borrar filtros

Creating a matrix with variables

3 visualizaciones (últimos 30 días)
Matt Lawson
Matt Lawson el 7 de Oct. de 2017
Editada: Kian Azami el 7 de Oct. de 2017
Hi Folks,
So I'm trying to create my own version of a continuous least squares. Here's a picture of what I'm attempting to do.. (these notes were taken in class, the integrals should be attached to all elements of the matrix, sorry)
I figured I would approach this problem by creating the first row of my matrix which was coded as
syms('x','a','b','n')
f = sqrt(x);
A = [int(x.^(n),a,b) int(x.^(n+1),a,b) int(x.^(n+2),a,b)];
b = [int(x.^(n)*f,a,b) int(x.^(n+1)*f,a,b) int(x.^(n+2)*f,a,b)];
N = 2;
And tested with n=0, which gave me the results I wanted. Now to use a for loop to create the rest of the matrix. I want n grow by 1 for each loop until I hit N, and I also want to add another row to my matrix each time my n goes up. I'm a simple man and figured the following code would work, but I keep getting the error:
Index exceeds matrix dimensions.
Error in sym/subsref (line 814) R_tilde = builtin('subsref',L_tilde,Idx);
when I run this
for i=1:N
A(i,:) A(i+1,:);
n = n+1;
end
Any advice, tips, tricks, etc would be appreciated!

Respuesta aceptada

Kian Azami
Kian Azami el 7 de Oct. de 2017
Editada: Kian Azami el 7 de Oct. de 2017
I think you can easily do this by saving your A variable to a new variable like B and a small change in your for loop:
n = 1;
for i=1:N
B(i,:) = A;
n = n+1;
end
Now each time the loop is run (which for your case is N = 2) the A variable is calculated by the new n and it will assign the A to a new raw of B.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by