The MATLAB Program (Please See M.File Attached)
Total Page:16
File Type:pdf, Size:1020Kb
Aqeel Ahmed
CENE 890 Homework # 2
Problem 1
Approximate the following definite integral using MATLAB. Notice that there is a singularity at x = 0. Hint: Use a combination of the functions find and isnan to isolate the point of singularity. Let N denote the number of points used to approximate the integral. Evaluate the integral for different values of N (e.g.,10,100, 1000, 10000) and estimate the error in your approximation.
Solution
The MATLAB Program (Please see m.file attached) clc n=1000; %The number of points x=-n:n/10000:n; index=find(x==0); x(index)=0.01; %Eliminating the NaN and integrating y = sin(x)./x; z = trapz(x,y) %plot(x,y)
% Exact Solution syms x F = sin(x)./x; Integral = int(F,-inf,inf)
Answer : pi
% The Error in ezch calculation using different N
Error = ((pi-z)/pi)*100
1. Error (N=10) -5.5737 2. Error (N=100) 0.5456 3. Error (N=1000) 0.0359 4. Error (N=10000) -0.0050
Problem 2
Find all the zeros of the following system of nonlinear equations. Look up MATLAB documentation to identify the appropriate functions. Aqeel Ahmed
Solution
Function file function [F] = nonlin(x) F = [x(1)^2/16 + x(2)^2/4 - 1; x(1)^2 - x(1) - x(2)-8];
Commands for Command Window x0 = [-5; 5]; % Make a starting guess at the solution options=optimset('Display','iter'); % Option to display output [x,fval] = fsolve(@nonlin,x0,options) % Calling the optimizer
Changing the initial guess x0 = [-2;2] x0 = [5; 5] x0 = [2; 2]
Answers are
1. x =
-2.6240 1.5095 2. x =
-2.6240 1.5095 3. x =
3.5314 0.9393 4. x =
3.5314 0.9393
Problem 3
Evaluate the following double integral using MATLAB. Search for an appropriate function.
Solution clc syms x y f = inline(vectorize(exp(-x*y)*sin(x*y)),'x','y') dblquad(f,-1,1,0,1) Aqeel Ahmed f =
Inline function: f(x,y) = exp(-x.*y).*sin(x.*y) ans =
-0.2218
Option 2 function z = integrnd(x,y) z = (exp(-x*y).*sin(x*y));
Q = dblquad(@integrnd,-1,1,0,1) (command for command window to get output)
Problem 4
You are given a function z of two vectors x and y in the interval −1≤ x ≤1,−1≤ y ≤1. You are asked to generate a smooth surface of the function z using bilinear and bicubic interpolation. The function is: z=sin(x2+y2) . Create two surface plots (using the function surf) that display the surface z based on these two interpolation methods. A smooth surface can be generated in MATLAB by decreasing the step size since we have an analytical function but we do not want to follow that approach! (We are allowed to use only interpolation). The original function is defined in intervals of 0.25 and looks choppy as shown below. We have to use interpolation to make it look smooth.
Solution
%Interpolation using the 'linear' and the 'cubic' methods.
[X, Y] = meshgrid(-1:.25:1); Z = sin(X.^2 + Y.^2); figure(1) surf(X,Y,Z)
[X1, Y1] = meshgrid(-1:.05:1); Z1 = interp2(X, Y, Z, X1, Y1, 'linear interpolation'); figure(2) surf(X1, Y1, Z1), title('Bilinear interpolation')
Z2 = interp2(X, Y, Z, X1, Y1, 'Cubic'); %Bi-cubic interpolation figure(3) surf(X1, Y1, Z2), title('Cubic interpolation') Aqeel Ahmed Aqeel Ahmed Aqeel Ahmed
Problem 5
The following system of equations, originally studie d by Edward Lorenz at MIT, is a famous system that mimics numerical weather prediction models. The equations are well- known because they are known to be highly sensitive to initial conditions – a known problem in weather prediction. The solutions can be plotted as a function of time or alternatively one solution can be plotted against another solution (this is the phase plane behavior). The phase plane plots are interesting because although the solutions show random behavior, their phase plane behavior appears “regular”. Solve the following Lorenz system using ode45 and reproduce the solutions given below, in particular, the “butterfly” phase plane solution. Use the plot command to generate the plots.
Solution clear [t1,A]=ode45(@fun,[0 30],[1 0 0]); X=A(:,1); Y=A(:,2); Z=A(:,3); [t2,A]=ode45(@fun,[0 30],[1 0.01 0.01]); %different initial values X1=A(:,1); Y1=A(:,2); Z1=A(:,3); [t3,A]=ode45(@fun1,[0 20],[1 0.01 0.01]); %different initial values,different function 27 replaced with 20 X2=A(:,1); Y2=A(:,2); Z2=A(:,3);
figure(1) plot(t1,X) hold on plot(t2,X1,'r-') xlabel('t'); ylabel('X') title('Time History with Initial Conditions')
figure(2) plot(X1,Z1) xlabel('X'); ylabel('Z') title('"Butterfly"') figure(3) plot(X2,Z2) xlabel('X'); ylabel('Z') title('Parameter Changed 27 to 20') Aqeel Ahmed
Function Files function dAdt = fun(t,A) dAdt = zeros(size(A)); x = A(1); y = A(2); z = A(3); dAdt(1) = 10*(y-x); dAdt(2) = x*(27-z)-y; dAdt(3) = x*y-(8/3)*z; function dAdt = fun1(t,A) dAdt = zeros(size(A)); x = A(1); y = A(2); z = A(3); dAdt(1) = 10*(y-x); dAdt(2) = x*(20-z)-y; dAdt(3) = x*y-(8/3)*z; Aqeel Ahmed Aqeel Ahmed