Final Cheat Sheet

Final Cheat Sheet - SIMPSON'S RULE W FUNCTION...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
SIMPSON’S RULE W/ FUNCTION FILE %Simpson's method for integral of 1/x from x=2 to x=5. % Note: we first have to define a user-defined function for 1/x result = quad( 'one_over_x' ,2,5); disp([ 'Integral of 1/x from x=2 to x=5 using Simpson''s method: ' ,num2str(result)]); disp( ' ' ); disp( ' ' ); %Newton-Cotes 8-panel method for integral of 1/x from x=2 to x=5. result = quad8( 'one_over_x' ,2,5); disp([ 'Integral of 1/x from x=2 to x=5 using Newton-Cotes method: ' ,num2str(result)]); %To use the Lobatto algorithm, you use the quadl function instead % of quad8. Otherwise the syntax is exactly the same. %function file function theResult = one_over_x(x) %ONE_OVER_X Calculates 1/x for x a scalar or a vector theResult = 1./x; FWD, BWKD, CEN DIFF APPROX METHODS x = [0:10]; y = [0 2 5 7 9 12 15 18 22 20 17]; n = length(x); %Calculate df that will be plotted in the forward and backward diffs df = diff(y)./diff(x); %Calculate df_c that will be plotted in the central diff estimate % (i.e., remember that whereas the forward and backward diff % estimates use n-1 data points, the central diff uses n-2, because % it can't use the first and last data points). %The formula here is essentially % f'(x_k) = (f(x_k_next) - f(x_k_prev))/(x_k_next - x_k_prev) df_central = (y(3:n)-y(1:n-2))./(x(3:n)-x(1:n-2)); %Create three subplots, plotting both the actual points and a line % through the points. Note that we define the axis to be the same % in each case so we can make a visual comparison of the results. subplot(3,1,1), ... plot(x(1:n-1),df,x(1:n-1),df, 'o' , ... ylabel( 'Derivative' ),title( 'Forward difference estimate' ), ... axis([0 10 -5 5]) subplot(3,1,2), ... plot(x(2:n),df,x(2:n),df, 'o' ), ... ylabel( 'Derivative' ),title( 'Backward difference estimate' ), ... axis([0 10 -5 5]) subplot(3,1,3), ... plot(x(2:n-1),df_central,x(2:n-1),df_central, 'o' ),xlabel( 'x' ), ... ylabel( 'Derivative' ),title( 'Central difference estimate' ), ... axis([0 10 -5 5]) LAW OF COSINES AND SYMBOLIC REPRESENTATION syms a b c A;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
%Define symbolic expression E = a^2 - b^2 - c^2 + 2*b*c*cos(A); %Solve for b result_for_b = solve(E,b); disp( 'Solving law of cosines for the length of side b:' ); disp(result_for_b); disp( ' ' ); %Part (b) %Substitute the given values of A = 60 degrees, a = 5 m, and % c = 2 m into the result_for_b equation result = subs(result_for_b,{A,a,c},{60*pi/180,5,2}); disp(result); INTERPOLATION time_m = [2 4 6 ? 10 12 14 ? 16 18 20 22 24]; temps_m = [10 12 18 24 21 17 9 13 9 7]; %Interpolation at specified times time_i = [8 16]; temps_i1 = interp1(time_m,temps_m,time_i, 'linear' ); temps_i3 = interp1(time_m,temps_m,time_i, 'spline' ); fprintf( 'Linear interpolated value for t = 10 minutes is %.1f degrees Celsius\n' ,temps_i1(1)); snn = inte rp1(f,spl,2500,’ne a re st’) % ne a re st ne ighbor inte rpola tion where nearest neighbor is another interpolation method, in which, as implied by its name,interpolates
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

Final Cheat Sheet - SIMPSON'S RULE W FUNCTION...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online