Readme

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Republic of the Philippines

Laguna State Polytechnic University


Province of Laguna

College of Engineering

Laboratory No. 3

Root Finding

Submitted by:

Pajadan, Neil Olsen M.

Section:

BSECE 4A

Date Submitted:February 13, 2019


Laboratory No. 1

Laboratory Title: Root Finding.

Learning Objectives: To familiarize each self to MatLab commands and be able


to run simple calculations and be capable of writing scripts and functions.

Equipment Required: Computer with MatLab software installed.


The SYMBOLIC MATH TOOLBOX installed in MatLab.
MATLAB Library Functions:
clc: CLC Clear command window. CLC clears the command window and
homes the cursor.
clear all: CLEAR Clear variables and functions from memory. CLEAR removes all
variables from the workspace. CLEAR VARIABLES does the same thing.
close all: CLOSE Close figure. CLOSE, by itself, closes the current figure window.
CLOSE ALL closes all the open figure windows.
exp: EXP Exponential. EXP(X) is the exponential of the elements of X, e to the X.
input: INPUT Prompt for user input. R = INPUT('How many apples') gives the user
the prompt in the text string and then waits for input from the keyboard. The
input can be any MATLAB expression, which is evaluated, using the variables in
the current workspace, and the result returned in R. If the user presses the return
key without entering anything, INPUT returns an empty matrix.
linspace: LINSPACE Linearly spaced vector. LINSPACE(X1, X2) generates a row
vector of 100 linearly equally spaced points between X1 and X2.
tan : Tangent of argument in radians. tan(X) is the tangent of the elements of X.
atan: is the arctangent of the elements of X.
sin: is the sine of the elements of X.
acos: acos(X) is the arccosine of the elements of X. Complex results are
obtained if ABS(x) > 1.0 for some element.
disp: disp(X) displays the array, without printing the array name. In all other ways
it's the same as leaving the semicolon off an expression except that empty
arrays don't display.
feval: (F,x1,...,xn) evaluates the function specified by a function handle or
function name, F, at the given arguments, x1,...,xn. For example, if F = @foo,
FEVAL(F,9.64) is the same as foo(9.64).

Page1
Problem/Activity:
Task 4.1: Modify the codes userfn.m and testplotm.m to determine the locations
of the roots of the function f(x)=xsinx between 0 and 10. Use the ginput
command to make sure that you have reasonable estimates of the locations of
the zeros. Notice the code userfn.m as it stands takes two input parameters,
whereas this function does not need any parameters and as such the function
can be changed to only have one input, namely x. You should also make sure
you use a dot in the calculation, since the function will be applied to vectors.
Task 4.2: Given that ex > 0 ∀x show that coshx has no zeros and sinhx has only
one. Consequently comment on the zeros of the function f(x)= coshm xsinhn x
where n,m ∈ N.
Task 4.3: Determine the location of the roots of the polynomial f(x)=𝑥 2 + bx +1 , b
∈ R, giving details of when you expect there to be no real roots, one roots or two
real roots. (Attempt this mathematically but then use MATLAB’s plotting routine
to verify your findings.)
Task 4.4: Consider the function f(x) = sinx + β cosx β∈ R and derive an expression
for the value of the zeros of f(x) mathematically. Use MATLAB to verify your
answer for certain values of β (perhaps using zero and one as a start).
Task 4.5: Modify the code eqn.m (page 112) to determine the zeros of the
function f(x) = 2cosx−sinx between −2π and 2π using the fixed point method.
(You may need to use a plot of the function to identify initial guesses for the
values.)
Task 4.6: Consider, using the fixed point method, the zeros of the function f(x)= 𝑥 2
+ bx +1and the two ways of rewriting the function in the required form as
g(x)=−( 𝑥 2 +1)/b and g(x)=− √−(𝑏𝑥 + 1). Using the results from Task 4.3 state
which method is appropriate (if either) for a given value of b. (Recall we require
|𝑔1 (x)| < 1 for the method to work.)

Page2
Task 4.7: Use the bisection method to determine the zero of the function
f(x)=2𝑥 2 − 𝑥 3 + sinx between one and three. You will need to enter thecodes
bisect.m, mbisect.m and func.m. The code bisect.m does not need
modification: however you need to change func.m to evaluate the function f(x)
at a point x (again remember to use the dots in the correct place). You also
need to change mbisect.m to reflect the fact that we are seeking a zero of the
function defined in func.m in the range 1 to 3.
Task 4.8: Consider the function f(x) = cos3x between 0 and π. Comment on the
bisection method using the ranges [0, π], [0, 7π/8] and finally [π/8, π]. It is not
necessary to write a code to solve this problem; you should sketch the curve
and work out what is likely to happen.
Task 4.9: Using the Newton–Raphson method calculate the roots of the functions
f(x)=x cosx − sinx and g(x)=( 𝑥 3 − x)sinx. These should be done as two separate
calculations. You will need to write two MATLAB functions for each case to
evaluate the function and its derivative. These should be called func.m and
func prime.m so that they can be called from Newton Raphson.m (alternatively
you could change the line in Newton Raphson.m to reflect that you are using
different function names).
Task 4.10: Using the method of False Position find the roots of the function
sin(x−𝑥 3 ) between x =0 and x =6 .
Task 4.11: Using the knowledge that the polynomial p(x)= 𝑥 3 − 4𝑥 2 + 5x − 2 has a
repeated root, determine its location.
Task 4.12: Using the command roots calculate the solutions of the equations
𝑥 3 + 𝑥 2 + x +1 = 0 and −2x + x5 + 𝑥 2 = 4.
Here you need to be careful that the coefficients of the polynomials are entered
correctly as vectors.
Task 4.13: Using the command fzero investigate the roots of the functions xsinx +
cosx, (𝑠𝑖𝑛𝑥)3 − sinx and (𝑥 2 − 4x + 5)/(x−1).

Page3
Task 4.14 (*): The solutions of the differential equation
𝑑2 𝑦 𝑑𝑦 1
𝑥2 + 𝑥 + (𝑥 2
− )𝑦 = 0
𝑑𝑥 2 𝑑𝑥 4
are given by 𝐽1 (𝑥) and 𝑌1 (𝑥), which are called Bessel functions. Using the MATLAB
2 2

command besselj(nu,x) (where nu is equal to 1/2 in this case) and fzero


determine the zeros of the function 𝐽1 (𝑥) in the interval (0,20].
2

Task 4.15 (D): The following codes are designed to find a root of the function
f(x)= x sin x − 𝑥 2 cos x −1 using the Newton–Raphson method

MatLab Script:
Task 4.1: function [value] = userf1.m(x)
value = x.*sin(x);
x = linspace(0,10);
y = feval('userf1.m',x);
plot(x,y)
grid on
Task 4.2:
Task 4.3: b = input('Value of b');

Page4
x = -10:0.1:10;
f = x.^2+b*x+1;
plot(x,f)
Value of b2
Task 4.4:
Task 4.5:

function g = eqn(x)
g = acps(sin(x)/2);
x=-2*pi:0.1:2*pi;
f=acos(sin(x)/2);
plot(x,f)
grid on
Task 4.6:
function g=eqn(x)
b=3;
g = -sqrt(-(b*x+1));
Task 4.7:
function[f]=func(x)
f=2*x.^2-x.^3+sin(x);
mbisect
Task 4.8:

Task 4.9:
x = 0:0.01:4.41;
f = x.*cos(x)-sin(x);
fp = -x.*sin(x);
f
fp

Task 4.10:
guess point 1 :0.8

Page5
guess point 2:1.2

guess point 1 :1.6


guess point 2 :1.8
Task 4.11:

f = x.^3-4*.^2+5*x+2;
fp = 3*x.^2-8*x+5;
fpp = 6*x.^2-8;
Task 4.12:
Task 4.13:
Task 4.14:

Task 4.15:
x=0;
in=x;
f(in) = in.*sin(in)-in.^2.*cos(in)-1;
fp(in) = (in+1)*cos(in)-2.*in.*sin(in);

Observation:

In this laboratory we’ve performed mathematical functions of finding the


roots of multiple equation but the difference because the roots are so many we
used the MatLab tool, and because we are not that familiar in most of the
commands in this laboratory I wasn’t able to complete the scripts. I observed
that rather than being careful on typing the scripts you also have to do some
better understanding of the process and do some critical thinking to be able to
come up with a MatLab script.

Page6

You might also like