Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a . with 1 1 grid points b . with 2 1 grid points c . with 4 1 grid points d . with 8

a. with 11 grid points
b. with 21 grid points
c. with 41 grid points
d. with 81 grid points
Fig 4. Results for all grid points for approach 2
Write a python code of the following matlab code. clc;clear;close all;
% Problem 2(c)-second approach
a =0;
b =3;
sigma =-5;
beta =3;
mplus1=[10204080];
f = @(x) exp(x);
% True solution
u_true = @(x) exp(x)+(sigma-exp(a))*x+beta-exp(b)...
+ b*exp(a)-b*sigma;
% 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_2c2(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 with 11 points is 1.64966e-01
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions