Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (4 points) Consider the following boundary value problems involving both firs

ID: 3605103 • Letter: 2

Question

2. (4 points) Consider the following boundary value problems involving both first and second derivative -u"(x)+u"(x) + u(x)=2sin(x) + cos(x), a(0)=0, u(1)=sin(1) xE [0, 1] The exact solution is u (x) sin(x) Use the finite difference scheme to solve the equation (1) with the setup in Matlab (see the code sent) h = 1/N; % mesh size = length of domain/N,N+1 is the number of grid points : 0:h:1; % grid points, x(1) and x(N-1) denote the boundary points a. What's the coefficient matrix for the unknown u? b. Write a Matlab code to implement the finite difference scheme c. Define the (infinity) error which can be computed by the command norm(u-sin(x), Inf) The convergence rate is defined by Errorh/Errorh/2- Complete the following table What's the convergence rate observed? Why? N mesh size h Errork raten 10 20 40 80

Explanation / Answer

MATLAB provides the limit function for calculating limits. In its most basic form, the limit function takes expression as an argument and finds the limit of the expression as the independent variable goes to zero.

For example, let us calculate the limit of a function f(x) = (x3 + 5)/(x4 + 7), as x tends to zero.

syms x

limit((x^3 + 5)/(x^4 + 7))

MATLAB will execute the above statement and return the following result

ans =

5/7

The limit function falls in the realm of symbolic computing; you need to use the syms function to tell MATLAB which symbolic variables you are using. You can also compute limit of a function, as the variable tends to some number other than zero. To calculate lim x->a(f(x)), we use the limit command with arguments. The first being the expression and the second is the number, that x approaches, here it is a.

For example, let us calculate limit of a function f(x) = (x-3)/(x-1), as x tends to 1.

limit((x - 3)/(x-1),1)

MATLAB will execute the above statement and return the following result

ans =

NaN

Let's take another example,

limit(x^2 + 5, 3)

MATLAB will execute the above statement and return the following result

ans =

14

MATLAB provides the diff command for computing symbolic derivatives. In its simplest form, you pass the function you want to differentiate to diff command as an argument.

For example, let us compute the derivative of the function f(t) = 3t2 + 2t-2

Example

Create a script file and type the following code into it

syms t

f = 3*t^2 + 2*t^(-2);

diff(f)

When the above code is compiled and executed, it produces the following result

ans =

6*t - 4/t^3

Following is Octave equivalent of the above calculation

pkg load symbolic

symbols

t = sym("t");

f = 3*t^2 + 2*t^(-2);

differentiate(f,t)

Octave executes the code and returns the following result

ans =

-(4.0)*t^(-3.0)+(6.0)*t

Verification of Elementary Rules of Differentiation

Let us briefly state various equations or rules for differentiation of functions and verify these rules. For this purpose, we will write f'(x) for a first order derivative and f"(x) for a second order derivative.

Following are the rules for differentiation

Rule 1

For any functions f and g and any real numbers a and b are the derivative of the function:

h(x) = af(x) + bg(x) with respect to x is given by

h'(x) = af'(x) + bg'(x)

Rule 2

The sum and subtraction rules state that if f and g are two functions, f' and g' are their derivatives respectively, then,

(f + g)' = f' + g'

(f - g)' = f' - g'

Rule 3

The product rule states that if f and g are two functions, f' and g' are their derivatives respectively, then,

(f.g)' = f'.g + g'.f

Rule 4

The quotient rule states that if f and g are two functions, f' and g' are their derivatives respectively, then,

(f/g)' = (f'.g - g'.f)/g2

Rule 5

The polynomial or elementary power rule states that, if y = f(x) = xn, then f' = n. x(n-1)

A direct outcome of this rule is that the derivative of any constant is zero, i.e., if y = k, any constant, then

f' = 0

Rule 6

The chain rule states that, derivative of the function of a function h(x) = f(g(x)) with respect to x is,

h'(x)= f'(g(x)).g'(x)

Example

Create a script file and type the following code into it

syms x

syms t

f = (x + 2)*(x^2 + 3)

der1 = diff(f)

f = (t^2 + 3)*(sqrt(t) + t^3)

der2 = diff(f)

f = (x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)

der3 = diff(f)

f = (2*x^2 + 3*x)/(x^3 + 1)

der4 = diff(f)

f = (x^2 + 1)^17

der5 = diff(f)

f = (t^3 + 3* t^2 + 5*t -9)^(-6)

der6 = diff(f)

When you run the file, MATLAB displays the following result

f =

(x^2 + 3)*(x + 2)

der1 =

2*x*(x + 2) + x^2 + 3

f =

(t^(1/2) + t^3)*(t^2 + 3)

der2 =

(t^2 + 3)*(3*t^2 + 1/(2*t^(1/2))) + 2*t*(t^(1/2) + t^3)

f =

(x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)

der3 =

(2*x - 2)*(3*x^3 - 5*x^2 + 2) - (- 9*x^2 + 10*x)*(x^2 - 2*x + 1)

f =

(2*x^2 + 3*x)/(x^3 + 1)

der4 =

(4*x + 3)/(x^3 + 1) - (3*x^2*(2*x^2 + 3*x))/(x^3 + 1)^2

f =

(x^2 + 1)^17

der5 =

34*x*(x^2 + 1)^16

f =

1/(t^3 + 3*t^2 + 5*t - 9)^6

der6 =

-(6*(3*t^2 + 6*t + 5))/(t^3 + 3*t^2 + 5*t - 9)^7

Following is Octave equivalent of the above calculation

pkg load symbolic

symbols

x=sym("x");

t=sym("t");

f = (x + 2)*(x^2 + 3)

der1 = differentiate(f,x)

f = (t^2 + 3)*(t^(1/2) + t^3)

der2 = differentiate(f,t)

f = (x^2 - 2*x + 1)*(3*x^3 - 5*x^2 + 2)

der3 = differentiate(f,x)

f = (2*x^2 + 3*x)/(x^3 + 1)

der4 = differentiate(f,x)

f = (x^2 + 1)^17

der5 = differentiate(f,x)

f = (t^3 + 3* t^2 + 5*t -9)^(-6)

der6 = differentiate(f,t)

Octave executes the code and returns the following result

f =

(2.0+x)*(3.0+x^(2.0))

der1 =

3.0+x^(2.0)+(2.0)*(2.0+x)*x

f =

(t^(3.0)+sqrt(t))*(3.0+t^(2.0))

der2 =

(2.0)*(t^(3.0)+sqrt(t))*t+((3.0)*t^(2.0)+(0.5)*t^(-0.5))*(3.0+t^(2.0))

f =

(1.0+x^(2.0)-(2.0)*x)*(2.0-(5.0)*x^(2.0)+(3.0)*x^(3.0))

der3 =

(-2.0+(2.0)*x)*(2.0-(5.0)*x^(2.0)+(3.0)*x^(3.0))+((9.0)*x^(2.0)-(10.0)*x)*(1.0+x^(2.0)-(2.0)*x)

f =

(1.0+x^(3.0))^(-1)*((2.0)*x^(2.0)+(3.0)*x)

der4 =

(1.0+x^(3.0))^(-1)*(3.0+(4.0)*x)-(3.0)*(1.0+x^(3.0))^(-2)*x^(2.0)*((2.0)*x^(2.0)+(3.0)*x)

f =

(1.0+x^(2.0))^(17.0)

der5 =

(34.0)*(1.0+x^(2.0))^(16.0)*x

f =

(-9.0+(3.0)*t^(2.0)+t^(3.0)+(5.0)*t)^(-6.0)

der6 =

-(6.0)*(-9.0+(3.0)*t^(2.0)+t^(3.0)+(5.0)*t)^(-7.0)*(5.0+(3.0)*t^(2.0)+(6.0)*t)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote