Plotting the graph of an exact solution against a discrete solution
9 views (last 30 days)
Show older comments
So the problem is asking to write a routine for the finite discrete solution with
-u''(x)=12x^2 ; domain x=[0,1] ; u'(0)=u(1)=0 ; deltax=1/(n+1)
I have a code but when I run it the plot for the exact solution is right but the discrete solution plot(marked by 'o') is way below it. I cannot figure out why it isn't closer (because I know it should be). Any help is appreciated. The first row of the matrix should be 1 -1 0 0 0..... because of the boundary conditions. Here is my code
function [delta_x] = Difference_Plot(n)
%Step size
delta_x = 1/(n+1);
%Discretized Domain
x_disc = (0:1/(n+1):1)';
%Toeplitz Matrix
T=zeros(n-1);
T(1,1)=1;
T(1,2)=-1;
for i=2:n-2
T(i,i-1)=-1;
T(i,i)=2;
T(i,i+1)=-1;
end
for i=n-1
T(i,i-1)=-1;
T(i,i)=2;
end
disp(T)
%The function
f = 12*(x_disc(2:n)).^2;
%Discrete Solution
u_disc = T\((delta_x)^2*f);
x_exact=x_disc;
%Exact solution
u_exact = -(x_exact(2:n)).^4+1;
x_exact = (0:1/500:1)';
u_exact = -(x_exact).^4+1;
%Plot of exact solution
plot(x_exact, u_exact, 'r')
hold on
%Discrete solution
plot(x_disc(2:n), u_disc, 'o')
hold off
0 Comments
Answers (0)
See Also
Categories
Find more on Line Plots 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!