170 likes | 516 Views
Flash Gordon and the Mud Men of Matlab. Quick Exercise (!). Consider Polynomial Addition again : how would you write a program that takes in two polynomials and irrespective of their sizes it adds the polynomials together ? Given that the function length(A) returns the length of a vector.
E N D
Quick Exercise (!) • Consider Polynomial Addition again : how would you write a program that takes in two polynomials and irrespective of their sizes it adds the polynomials together ? Given that the function length(A) returns the length of a vector. Answers on a postcard to : dgordon@maths.kst.dit.ie oh, and while you’re here anyhow, if you have a browser open, please go to the following sites : http://www.the hungersite.com http://www.hitsagainsthunger.com
Creating Programs Title : Program.m C: function out = program(inputs) % PROGRAM <code>
Know Thyself • Where am I ? • pwd • Get me onto the hard disk • cd C: • Where am I now ? • pwd • Get me to where I know • cd ..
Quick Answer (!) function c = mypoly(a,b) % MYPOLY Add two polynomials of variable lengths % mypoly(a,b) add the polynomial A to the polynomial % B, even if they are of different length % % Author: Damian Gordon % Date : 3/5/2001 % Mod'd : x/x/2001 % c = [zeros(1,length(b) - length(a)) a] + [zeros(1, length(a) - length(b)) b];
Recursion function b = bart(a) %BART The Bart Simpson program writes on the blackboard % program, Bart writes the message a few times % and then goes home to see the Simpsons if a == 1 disp('I will not....'); else disp('I will not skateboard in the halls'); bart(a - 1); end
Curve Fitting • What is the best fit ? • In this case least squares curve fit • What curve should be used ? • It depends...
POLYFIT : Curve Fitting • polyfit(x,y,n) - fit a polynomial • x,y - data points describing the curve • n - polynomial order • n = 1 -- linear regression • n = 2 -- quadratic regression
Curve Fitting Example x = [0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1]; y = [-.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2]; polyfit(x,y,n) • n = 1 p = 10.3185 1.4400 • n = 2 p = -9.8108 20.1293 -0.0317 y = -9.8108x2 + 20.1293x - 0.0317
Curve Fitting Example xi = linspace(0,1,100); z = polyval(p,xi) plot(x,y,'o',x,y,xi,z,':');
Interpolation - 1D • t = interp1(x,y,.75) t = 9.5200 also • interp1(x,y,.75,’spline’) • interp1(x,y,.75,’cubic’)
Interpolation - 2D • interp2(x,y,Z,xi,yi,TYPE) TYPE = 'nearest' - nearest neighbor interpolation 'linear' - bilinear interpolation 'cubic' - bicubic interpolation 'spline' - spline interpolation
fft fft2 ifft ifft2 filter filter2 fftshift Fast fourier transform 2-D fft Inverse fft 2-D Inverse fft Discrete time filter 2-D discrete tf shift FFT results so -ve freqs appear first Fourier Functions
Tensors • See ‘Programs’ • ‘Tensors’ • ‘Tensors.html’
3D Graphics T = 0:pi/50:10*pi; plot3(sin(t), cos(t), t);
3D Graphics title('Helix'), xlabel('sin(t)'), ylabel('cos(t)'), zlabel('t') grid
3D Graphics • Rotate view by elevation and azimuth view(az, el); view(-37.5, 60);