Borrar filtros
Borrar filtros

How to output all these row vectors into a matrix?

3 visualizaciones (últimos 30 días)
Walter Nap
Walter Nap el 9 de Oct. de 2017
Comentada: Walter Nap el 9 de Oct. de 2017
Hello, I am almost finished with an assignment for the course Linear Algebra but I am stuck with the very last part of this one question. We had to create a loop inside a function that would result in (for my case) 225 iterations and thus 225 vectors x in R2. Now we have to transpose these column vectors x into row vectors and make a matrix X that consists of 225 rows with each row being a row vector x. Eventually, we have to plot this matrix with the first column on the x axis and the second column on the y axis. My question is, how to get this output matrix? I have been looking over the internet but every situation seems to differ from mine and can't figure it out. Thank you very much in advance! This is what I already have:
function X=Exercise4(v);
v(1)=2; v(2)=0; v(3)=1; v(4)=4; v(5)=2; v(6)=8; v(7)=0;
v=[v(1);v(2);v(3);v(4);v(5);v(6);v(7)];
%Fill in and create all the matrices given
S=[1,v(4)/5;0,1];
a=pi*(v(2)+12)/300;
R=[cos(a),-sin(a);sin(a),cos(a)];
r=0.97;
A=r*S*R*(S^-1);
iter=0;
x=[v(5)+1;v(6)]; %This is the first value of x (x(0)) that was given in the question
MaxNorm=0.01; %The length of the vector could not be smaller than 0.01
while norm(x)>=MaxNorm
x=A*x
iter=iter+1 %Count the number of iterations
end
disp('Iterations'); %Display the number of iterations
disp(iter);

Respuesta aceptada

KL
KL el 9 de Oct. de 2017
You just need to store your x values inside the loop in an array. Anyway, firstly when you write
v(1)=2; v(2)=0; v(3)=1; v(4)=4; v(5)=2; v(6)=8; v(7)=0;
you are already creating a vector so you don't need the line that follows. Or you could simply write
v = [2 0 1 4 2 8 0];
Both mean the same thing.
Nextly, you could save the given values like,
x_init=[v(5)+1;v(6)]; %This is the first value of x (x(0)) that was given in the question
MaxNorm=0.01; %The length of the vector could not be smaller than 0.01
and then define iter as 1 to calculate the first iteration outside the loop like,
iter=1;
x(:,iter) = A*x_init;
and then go inside the loop to calculate the remaining iterations until the condition is satisfied,
while norm(x(:,iter))>=MaxNorm
iter=iter+1; %Count the number of iterations
x(:,iter)=A*x(:,iter-1);
end
finally, transpose the matrix using,
x = x.';
then plot or do whatever you want to do with it,
plot(x)

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by