1 Lab 2a: Numerically solving equations
As usual you will not submit Lab2a, but it will serve as a preparation for your lab quiz this week, and for next weeks Lab2b. In this lab you will be looking at root finding using the bisection method and fixed point iteration to solve
f(x) = 0 (1) First we will implement some of some functions which we are going to solve.
Task 1: Functions
Copyright By PowCoder代写 加微信 powcoder
Your first task will be to create function files to evaluate the functions: 1. g(x) = 1 x + 2 . Save this function as myexample1.m
2. g(x) = sech2(x). Save this function as myexample2.m
Next use the fplot command to plot example1 on the same axes as y = x:
>> ,[-3,3]) >> hold on
>> x, [-3,3])
>> hold off
Then you can do the same for @myexample2 but on the interval [0,pi]. Note that the solutions for x = g(x) for these 2 functions are the intersections of y = x with y = g(x) in the plots.
The character @ followed by a function name is known as a function handle. They are used to pass functions to other functions, in the first line of the above example we are passing the function handle @myexample1 of your function myexample1 to the plotting function fplot.
Save these plots as myexamplePlot1.pdf and myexamplePlot2.pdf respectively.
Task 2: Bisection Method
You try some problems in the Assigned Problems 1.1 / 1a, 3a, 5ab Computer Problems/ 1a, 5a
First use Matlab as a calculator to execute bisection (this helps you prepare for the Quiz and midterm). Then also apply the Book’s routine bisect.m which can be found at the Matlab Code section of:
https://media.pearsoncmg.com/aw/aw_sauer_num_analysis_3/main.html
Task 3: Fixed Point Iteration – simplest approach
Fixed point iteration solves this equation by rearranging this equation into the form
x = g(x) (2)
First just simply use Matlab as a calculator. Here just apply the functions recursively. You might also try some problems in the AssignedProblems list for 1.2 (including the computer problems).
Task 4: Fixed Point Iteration – adding bells and whistles
Start simple and then gradually introduce more features into your code. 1
Now we will implement the fixed point iteration algorithm.
Below is a header for your fixed point iteration code, copy this header, suitably updated
with your name and the current date, and save it as myfpi.m function [xs,flag]=myfpi(g,x0,tol,maxiter)
Author: YourFirstName YourLastName
Date: Today
Purpose: Compute approximate solution to f(x)=0 via fixed point iteration
g — A function handle for x=g(x) corresponding to f(x)=0 x0 — Initial guess for the fixed point
tol — Tolerance of the solution.
maxiter — Maximum number of iterations.
xs — Approximation to the fixed point
flag — Flag specifying if the solution has been obtained:
= The number of iterations taken to converge.
= -1 If the algorithm has not converged in maxiter iterations.
The first step is to initialize the output arguments e.g. flag = =1
The next step is to create a for loop which updates the new value of x via the fixed point iteration step
xnew = g(xold) (3)
Next implement a tolerance test using an if-else statement. You should use the break
command to exit from the loop.
In your if-else statement add a line which sets the flag to the current number of iterations if the iteration converges.
You should now have a working fixed point iteration code.
Test your fixed point iteration code with the example functions above which you wrote in
task 1, for example
>> ,0.5,1e-6,100)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com