Multiple Iterations over a system of linear equations

3 Ansichten (letzte 30 Tage)
JD
JD am 17 Okt. 2020
Kommentiert: JD am 17 Okt. 2020
Hello all. I am trying to solve the system of linear equations define by CX = K over multiple iterations (300*delta_t). But my plot only plots the first time step. Can someone help me with understanding how I can fix this in my code below?
Thanks so much!
clear all
close all
clc
N=5;
u(1:1:N) = 0;
u(N+1) = 1;
delta_t = 20;
Below is the for loop im having trouble with
for t = 1:delta_t:300*delta_t
G(t) = t/20;
A(t) = G(t)/2;
B(t) = 1 + G(t);
for j = 2:1:N
k(j) = ((1-G(t))*u(j))+((G(t)/2)*(u(j+1)+u(j-1)));
end
C = [A(t) B(t) 0 0;
A(t) B(t) A(t) 0;
0 A(t) B(t) A(t);
0 0 A(t) B(t)];
k = [k(2); k(3); k(4); k(5)-A(t)];
X = C\k;
x1 = [0; X; 1];
y = 0:1/5:1;
plot(x1,y)
end

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Okt. 2020
u(1:1:N) = 0;
u(N+1) = 1;
So all of your u values are 0 except for the last one
k(j) = ((1-G(t))*u(j))+((G(t)/2)*(u(j+1)+u(j-1)));
Since all of your u are 0 except for the last one, u(j) is going to be 0 throughout that loop, and u(j+1)+u(j-1) is going to be 0 except when j = N at which point you are indexing u(N+1)+u(N-1) which would be 1-0 which would be 1. 0 times anything is 0, so k(j) is 0 except for when j = N.
For the last case, j = N, we can see that k(N) = G(t)/2 * (1-0) = G(t)/2
A(t) = G(t)/2;
%...
k = [k(2); k(3); k(4); k(5)-A(t)];
We know that k(2), k(3), k(4) are all 0, and that k(5) = G(t)/2 and A(t) = G(t)/2 . G(2)/2 - G(t)/2 = 0. Therefore you are replacing k with a vector of 4 zeros.
The solution for C\k when k is all zero is going to be a vector of 0.
Therefore your solutions are all the same for every iteration, so you are going to end up plotting the same line many times.
  1 Kommentar
JD
JD am 17 Okt. 2020
Thank you Walter. I need to update the value of the X matrix with each iteration!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Rafael Hernandez-Walls
Rafael Hernandez-Walls am 17 Okt. 2020
I think the problem is where the plot function, maybe you need to put another command to plot in each iteration, something like this:
...
plot(x1,y)
drawnow
end
  1 Kommentar
JD
JD am 17 Okt. 2020
Hi Rafael,
Thank you for the response. However, that does not work. I still only get 1 plot.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by