Revised 2005-Nov-18
Full documentation is available from the "Help" button in MATLAB itself, or from the Mathworks website.
Please tell the Professor if you have suggestions for improvements to this documentation.
function psi = bump(x,a) C=1; psi = C .* 1./(1+(x./a).^2); return(Note that this version is not properly normalized.)
bump([1.5, 2.0, 2.5], 2.5)it will return a vector of values of the function at those positions:
0.3710 0.3077 0.2523Technically, you only need dots in front of ^ operators for which the first argument might be a vector, and * and / operators for which the quantities on each side of the operator could both be vectors. The MATLAB function quad gives a vector argument to the function it is integrating, so vectors may crop up when you don't expect them. The safest thing is to put a dot in front of every * /, and ^ operator in all your functions.
??? Error using ==> mpower Matrix must be square.If somewhere you have written a*b instead of a.*b, you may get
??? Error using ==> mtimes Inner matrix dimensions must agree.The hardest one to debug is if you have used / instead of ./ somewhere in a function. If you integrate the function, this may cause quad to give a very odd error message:
??? Attempted to access y(7); index out of bounds because numel(y)=1.
sqrt(2)*cos(2*pi) 1.4142
fplot(@cos,[-pi,pi])To specify the y-limits of the plot as well as the x-limits, just include them in the limits vector:
fplot(@sin, [-pi,pi,-2,2])You can also explicitly construct a list of values of x and values of the function at those points, and pass them to the plot function:
xrange = -pi : 0.1 : pi plot(xrange,cos(xrange))(The first line makes a list of values of x from -pi to pi in steps of 0.1.)
function s = sqr(x) s = x.*x; returnwhich could then be plotted with
fplot(@sqr,[-2,2])you can just write
fplot(@(x) x.*x ,[-2,2])where the expression @(x) x.*x is an unnamed ("anonymous") function definition for calculating the square of its argument. This is particularly useful if you want to use a multi-argument function. For example, suppose you have an M-file for the "pwr" function
function s = pwr(x,n) s = x.^n; returnYou can plot various polynomials over the range x=-2 to +2 by typing
fplot(@(x) pwr(x,2), [-2,2]) fplot(@(x) pwr(x,3), [-2,2]) fplot(@(x) pwr(x,2) - pwr(x,3)/2, [-2,2])To make a simultaneous plot of multiple functions, create an anonymous function that returns their values in an array. Eg:
fplot(@(x) [sin(x),cos(x)],[0,2*pi,-2,2])
fplot('pwr(x,2)', [-2,2])
fplot('pwr(x,2)-pwr(x,3)/2', [-2,2])
quad(@sqr, -2, 2)(quad stands for "quadrature", an old term for integration). Again, you can use an anonymous function instead,
quad(@(x) x.*x , -2, 2)Unfortunately, quad cannot deal with infinite limits of integration: you just have to make them very large but finite (see below).
>> exp(i*pi/4) ans = 0.7071 + 0.7071iNote that older versions of MATLAB may only understand j, not i. You can obtain real and imaginary parts of any expression using the real and imag functions. MATLAB functions can accept complex arguments, so you can write things like
quad( @(x) exp(3*j*x).*exp(-(x+1).^2),-10^6,10^6)and you get a complex answer.