Multiple Iterations over a system of linear equations
2 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Walter Roberson
2020-10-17
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 个)
Rafael Hernandez-Walls
2020-10-17
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!