Multiple Iterations over a system of linear equations

조회 수: 1 (최근 30일)
JD
JD 2020년 10월 17일
댓글: JD 2020년 10월 17일
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
  댓글 수: 1
Ameer Hamza
Ameer Hamza 2020년 10월 17일
Can you write down your problem in mathematical form?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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
JD
JD 2020년 10월 17일
Thank you Walter. I need to update the value of the X matrix with each iteration!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Rafael Hernandez-Walls
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
  댓글 수: 1
JD
JD 2020년 10월 17일
Hi Rafael,
Thank you for the response. However, that does not work. I still only get 1 plot.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by