how to arrange below code
Show older comments
i need to put the u1 u2 u3s in a loop and at the same time these i am trying to add 100 at the top and bottom of these matrixes.so that these 19*1 matrices becomes 21*1 matrices.the i want to plot for each u values.
clc;
clear;
n = 20;
L = 1;
dx = L/n;
%%
c = 1;
C = 0.1;
dt = C*dx/c;
%%
cond = 0.0136;
b = cond*dt/(dx*dx);
%%
aa(1:n-2)=-b;
bb(1:n-1)=1.+2.*b;
cc(1:n-2)=-b;
%%
MM=(diag(bb,0)+diag(aa,-1)+diag(cc,1));
uu=zeros(19,1);
uu(1) = b*100;
uu(19) = b*100;
u1 = inv(MM)*uu
u2 = inv(MM)*(uu+u1)
u3 = inv(MM)*(uu+u2)
u4 = inv(MM)*(uu+u3)
u5 = inv(MM)*(uu+u4)
u6 = inv(MM)*(uu+u5)
u7 = inv(MM)*(uu+u6)
Answers (1)
With some guessing:
ux = zeros(19, 7);
ux(:, 1) = MM \ uu; % Do not use inv(), see: doc inv
for k = 2:7
ux(:, k) = MM \ (uu + ux(:, k-1));
end
pad = repmat(100, 1, 7);
result = cat(1, pad, ux, pad);
Now result contains the column vectors.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!