Question: Problem 1 ( c ) : Results a . Grid of 1 1 points b . Grid of 2 1 points c . Grid of

Problem 1(c): Results
a. Grid of 11 points
b. Grid of 21 points
c. Grid of 41 points
d. Grid of 81 points
Fig 1: Results for all grids
Following table is generated:
h error ratio observed order
0.300004.77973e-02 NaN NaN
0.150001.19896e-023.986541.99514
0.075003.00274e-033.992911.99744
0.037507.51011e-043.998261.99937
Write a python code to the following matlab code.
clc;clear;close all;
% Problem1(c)
a =0;
b =3;
sigma =1;
beta =3;
mplus1=[10204080];
f = @(x) exp(x);
% True solution
u_true = @(x) exp(x)+(beta-sigma+exp(a)-exp(b))/(b-a)*x ...
+(b*sigma-a*beta+a*exp(b)-b*exp(a))/(b-a);
% True solution on fine grid for plotting
xfine = linspace(a,b,101);
ufine = u_true(xfine);
% Solve for multiple grids
hvals = zeros(numel(mplus1),1);
error = zeros(numel(mplus1),1);
for i =1:numel(mplus1)
% run routine for finite diff solve
[x,hvals(i),u]= finite_difference_1c(a,b,sigma,beta,mplus1(i),f);
% get error at grid points
u_hat = u_true(x);
err = u - u_hat;
error(i)= max(abs(err));
disp('')
disp(sprintf('Error with %i points is %9.5e',mplus1(i)+1,error(i)))
clf
plot(x,u,'s')% plot computed solution
title(sprintf('Computed solution with %i grid points',mplus1(i)+1));
hold on
plot(xfine,ufine)% plot true solution
xlabel('x')
ylabel('U')
hold off
drawnow
input('Hit to continue ');
clear x u
end
error_table(hvals,error); % print tables of errors and ratios
error_loglog(hvals,error); % produce log-log plot of errors and least squares
fit
 Problem 1(c): Results a. Grid of 11 points b. Grid of

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!