N = 6; %Set number of subintervals
h = 1/N; %Length of subintervals
diag( ones(N,1))
ans =
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
N*(diag( 2*ones(N,1)) -diag( ones(N-1,1)),1 )-diag( ones(N-1,1)),-1 ))
N*(diag( 2*ones(N,1)) -diag( ones(N-1,1)),1 )-diag( ones(N-1,1)),-1 ))
{Error: Invalid expression. When calling a function or
indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
}
N*(diag( 2*ones(N,1)) -diag( ones(N-1,1),1 )-diag( ones(N-1,1),-1 ))
ans =
12 -6 0 0 0 0
-6 12 -6 0 0 0
0 -6 12 -6 0 0
0 0 -6 12 -6 0
0 0 0 -6 12 -6
0 0 0 0 -6 12
K=N*(diag( 2*ones(N,1)) -diag( ones(N-1,1),1 )-diag( ones(N-1,1),-1 ))
K =
12 -6 0 0 0 0
-6 12 -6 0 0 0
0 -6 12 -6 0 0
0 0 -6 12 -6 0
0 0 0 -6 12 -6
0 0 0 0 -6 12
spy(K)
[Warning: MATLAB has disabled some advanced graphics
rendering features by switching to software OpenGL. For
more information, click here.]
spy
det(K)
ans =
3.2659e+05
cond(K)
ans =
19.1957
b=h*ones(N-1,1)
b =
0.1667
0.1667
0.1667
0.1667
0.1667
uh==K\b
{Undefined function or variable 'uh'.
}
uh=K\b
{Error using \
Matrix dimensions must agree.
}
uh=K/b
{Error using /
Matrix dimensions must agree.
}
size(K)
ans =
6 6
size(b)
ans =
5 1
K=N*(diag( 2*ones(N-1,1)) -diag( ones(N-2,1),1 )-diag( ones(N-2,1),-1 ))
K =
12 -6 0 0 0
-6 12 -6 0 0
0 -6 12 -6 0
0 0 -6 12 -6
0 0 0 -6 12
uh=K\b
uh =
0.0694
0.1111
0.1250
0.1111
0.0694
1:10
ans =
Columns 1 through 9
1 2 3 4 5 6 7 8 9
Column 10
10
1:2:10
ans =
1 3 5 7 9
x = h:h:1-h; %A vector containing the node positions except 0 and 1
x
x =
0.1667 0.3333 0.5000 0.6667 0.8333
exp( [0,1])
ans =
1.0000 2.7183
exp( [0,2].^2)
ans =
1.0000 54.5982
exp( [0,2]^2)
{Incorrect dimensions for raising a matrix to a power. Check
that the matrix is square and the power is a scalar. To
perform elementwise matrix powers, use '.^'.
}
b = h*exp(x.^2);
b
b =
0.1714 0.1863 0.2140 0.2599 0.3338
b'
ans =
0.1714
0.1863
0.2140
0.2599
0.3338
K\b'
ans =
0.0860
0.1435
0.1700
0.1607
0.1082
uh=K\b'
uh =
0.0860
0.1435
0.1700
0.1607
0.1082
plot(uh)
plot([0;uh;0])
plot(x,uh)