Sie sind auf Seite 1von 6

Computer-Aided Engineering (MECH 2220) Final Review

Numerical Differentiation. Which formulas were used for each? Write the algebraic formula beside each UWF.
function out= Fst___diff(x,y) %Fst______diff(x,y)calculates the (forward/backward/centered) difference %Returns a two column matrix if length(x)~=length(y) error('x and y must be the same length') end i=1; L=length(x); while i<=L-1 out(i,1)=x(i); out(i,2)=(y(i+1)-y(i))/(x(i+1)-x(i)); i=i+1; end out(L,1)=x(L); out(L,2)=NaN;

b.

function out= Fst______diff(x,y) %Fst______diff(x,y)calculates the (forward/backward/centered) difference %Returns a two column matrix if length(x)~=length(y) error('x and y must be the same length') end i=2; L=length(x); out(1,1)=x(1); out(1,2)=NaN; while i<=L out(i,1)=x(i); out(i,2)=(y(i)-y(i-1))/(x(i)-x(i-1)); i=i+1; end

c.
function out= Fst_____diff(x,y) %Fst______diff(x,y)calculates the (forward/backward/centered) difference %Returns a two column matrix if length(x)~=length(y) error('x and y must be the same length') end i=2; L=length(x); out(1,1)=x(1); out(1,2)=NaN; while i<=L-1 out(i,1)=x(i); out(i,2)=(y(i+1)-y(i-1))/(x(i+1)-x(i-1)); i=i+1; end out(L,1)=x(L); out(L,2)=NaN;

d. Fill in the blanks in order for this function to properly calculate the 2nd order derivative using the centered method.
function out= Sec_CENT_diff(x,y) if length(x)~=length(y) error('____________________') %Why did we do this? end i=__; L=length(x); out(1,1)=x(1); out(1,2)=NaN; while i<=L-1 out(i,1)=x(i); out(i,2)=(y(i+1)-2*____+y(___))/(x(__)-x(___))^2; i=i+1; end out(L,1)=x(L); out(L,2)=NaN; Numerical Integration Trapezoid Method. Fill in the blanks so that the function estimates the integral of y*dx using the trapezoid method. function Area=trap_method(x,y) L1=length(x); L2=length(y); if L1_____ error('the length of the two vectors must be the same') end i=______; Area=______; while i<=L1-1 Area=____+((y(i+1)+y(i))____)*(x(i+1)-____); i=____; end Numerical Integration Simpson's Method. function Area=Simpsons_1_3(x,y) L1=length(x); L2=length(y); %What are the 3 requirements to use Simpson's Method?? (IF...) to check for them. > > > > > > > > > > end sum_deltax=sum(deltax1);

Write 3 control structures

avg_deltax=(sum_deltax)/(L1-1); %interior even z=2;sum_IE=0; while z<=L1-1 sum_IE=sum_IE+y(z); z=z+2; end %interior odd w=3;sum_IO=0; while w<=L1-2 sum_IO=sum_IO+y(w); w=w+2; end Area=(deltax____)*(____+2*_______+4*______+y( end ));

Bisection Method for Finding Zeros. Estimate the zero for this equation, with bounds [ and up to 150 iterations.

], tolerance of 0.005,

A(1)=__ ______; n=1; ___________; ___________; file=fopen('bisection.txt','w'); eq1=@(_______________________; if eq1(A(1))*eq1(B(1))>0 _______________________________ %Hint: are these bounds acceptable? Why/why not? elseif eq1(A(1))==0 fprintf(file,'the root is %8.3f',A(1)) elseif eq1(B(1))==0 _______________________________________ else while n<=________ && abs(__________/__)>tol P(n)=(A(n)+B(n))/2; if _______________________ _______________________ _______________________ elseif eq1(P(n))*eq1(B(n))<0 A(n+1)=P(n); B(n+1)=B(n); elseif eq1(P(n))==0 fprintf(file,'the root is %8.7f',P(n)); end n=n____; end fprintf(file,'\n'); end fprintf(file,'\tThe root to the equation f= ex-(x^2 + 4)=0 on [2,3] was found to be %8.6f where f(%8.6f)=%8.6f.',P(n-1),P(n-1),eq1(P(n-1))); fprintf(file,'\n');

Curve-Fitting and Interpolation. %Given the following data: x = [0,1.1,3,4.2,6,7.1,12,14,15.5,100]; y = [-1,0,1,4,6,7,11,16,20,201]; %Graph the data vs. a linear interpolation (not linear regression use interp1), 2nd order polynomial derived with polyfit, 10th order polynomial from polyfit, and spline interpolation. Also, estimate y when x =25 using the four methods assigned above. Syntax: polynomial_vector = polyfit(x,y,n) Returns a vector that describes a polynomial of degree n in the form polynomial_vector(1)*x.^n+polynomial_vector(2)*x.^(n-1)+... +polynomial_vector(n)*x+polynomial_vector(n+1) interpolated_ys = interp1(x_data,y_data,x_to_evaluate_at,'linear' OR 'spline') For plotting, make x_to_evaluate_at nice and dense, like x_to_evaluate_at = 0:.01:100; Ordinary Differential Equations. Solve the following ordinary differential equation in Matlab by Euler's Method and by Runge-Kutta 4th Order using dx = .5, y(0) = 1, and calculating from x = 0 to x = 10: (You don't have to write a general script to perform Euler's Method / RK, though you can.)

Solve the following higher-order differential equation with ODE45():

MISC. - Control Structures, File Reading, &c. To write to a file, first file_ID = fopen('Filename.txt','w');

%then fprintf(file_ID,'Your text here %1.2f.\n',yourvariable) User-written functions: function [output1,output2,output3,...,outputn] = myfunctionname(input1, ...,inputn) %Comment here. output1=287*input1/input2+input8; %Assign all of the outputs. ... ... outputn = 23+input3; end %Save your file as myfunctionname. if <CONDITION1> task1;

task2; else if <CONDITION2> task3; else %All remaining possible conditions. task4; end for counting_variable = starting:step:ending_index task1; end while <CONDITION, usually based off of i, but not always> task1; i = i+1; %Usual way of counting, but could count down: i = i-1, &c. end Don't confuse dot multiplication, dot exponents, and dot division with matrix algebra. If you want to produce a vector containing the square of each term of x = [1,2,3,4,5], do >> x.^2 ans = [1,4,9,16,25]

How do you produce a vector myanswer from x = [1,2,4,8,16,32] and y = [1,2,3,4,5] such that, for every i, myanswer(i) = ((x(i)+3y(i)/x(2))^3)*x(i) without using loops?

Das könnte Ihnen auch gefallen