clear; clc; close ; format ; all

advertisement
clear; clc; close all; format shortg;
%Today we'll cover solving for zeroes, solving for max/min,
%numerically calculating integrals, and solving differential equations.
%Solving for zeroes is done with the fzero command.
%The fzero command finds the zero of a function nearest to your guess.
%You can enter a function three ways
%You can enter it directly into the function
%Ex: f(x) = (x-2)^3
x = fzero('(x-2)^3',1)
%You can also create an anonymous function as we did in assignment 7
%Then enter the name of the function
%Ex: f(x) = sin(x)-cos(x)
fun1 = @(x) sin(x)-cos(x);
x = fzero(fun1,1)
%Lastly you can create a function file.
%When you enter the name of the function file you must put an '@' symbol in
%front of the name
%Ex: f(x) = tan(x) - sec(x)
x = fzero(@testfun,1)
%Next we'll look at the fminbnd command. This command can be used to find
%the local minimum on a given interval
%Ex: f(x) = x^3-x for 0 <= x <= 2
[x fmin] = fminbnd('x^3-x',0,2)
%Again you can enter the name of a function as well.
[x fmin] = fminbnd(fun1,0,2)
%We can also use this function to find the maximum value.
%To do find the max of f(x) we find the minimum of -f(x) and multiply by -1
%Ex: Find the maximum value of f(x) = e^(-x^2+1) on
%You might want to graph the function first to get an idea of where the max
%might be
x = [-10:.1:10]; y = exp(-x.^2+1);
plot(x,y)
[x fx] = fminbnd('-exp(-x^2+1)',-2,2);
fmax = -fx
%Now we'll learn how to use matlab to numerically solve some calculus
%problems
%Numerical integrals using the quad function
%Ex: f(x) = 1+x, a = 0, b = 2
fun2 = @(x) 1+x;
q1 = quad('1+x',0,2)
%Entering directly
q2 = quad(fun2,0,2)
%Anonymous function
q3 = quad(@testfun2,0,2) %Function file
1
%Finally we can solve differential equations
%Ex: dy/dx = (1-y)/(1+x), approximate y for 0<=x<=8 with y(0) = 2
%Step 1: Define you anonymous function (or function file)
dydx = @(x,y) (1-y)/(1+x);
%Step 2: Choose which ODE method to use (ode45)
%Step 3: run the solver over the given x span and intial data
[x y] = ode45(dydx,0:.1:8,2);
%Hints:
%I don't have a lot of hints for you guys this week. Look for similar
%sample problems and read the section on ODEs and numerical integrals.
%Problem 2: Remember if you want to solve f(x) = a. Find the zero of
%g(x) = f(x) - a
%Problem 4: Try graphing the function first to find out how many 0's you
%should have. Then find a starting point near where you think each 0 is.
%Problem 9: See my work above to find a max value
%Problem 22 and 30: Read the sections and look at sample problems.
x =
2
x =
0.7854
x =
1.5708
x =
0.57734
fmin =
-0.3849
x =
4.8379e-05
fmin =
-0.99995
2
fmax =
2.7183
q1 =
4
q2 =
4
q3 =
4
Published with MATLAB® R2014b
3
function [ y ] = testfun( x )
%UNTITLED8 Summary of this function goes here
%
Detailed explanation goes here
y = tan(x) - sec(x);
end
Error using testfun (line 5)
Not enough input arguments.
Published with MATLAB® R2014b
1
function [ y ] = testfun2( x )
%UNTITLED Summary of this function goes here
%
Detailed explanation goes here
y = 1+x;
end
Error using testfun2 (line 6)
Not enough input arguments.
Published with MATLAB® R2014b
1
Download