The MATLAB Program (Please See M.File Attached)

The MATLAB Program (Please See M.File Attached)

<p> Aqeel Ahmed</p><p>CENE 890 Homework # 2</p><p>Problem 1</p><p>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.</p><p>Solution</p><p>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)</p><p>% Exact Solution syms x F = sin(x)./x; Integral = int(F,-inf,inf)</p><p>Answer : pi</p><p>% The Error in ezch calculation using different N</p><p>Error = ((pi-z)/pi)*100</p><p>1. Error (N=10) -5.5737 2. Error (N=100) 0.5456 3. Error (N=1000) 0.0359 4. Error (N=10000) -0.0050</p><p>Problem 2</p><p>Find all the zeros of the following system of nonlinear equations. Look up MATLAB documentation to identify the appropriate functions. Aqeel Ahmed</p><p>Solution</p><p>Function file function [F] = nonlin(x) F = [x(1)^2/16 + x(2)^2/4 - 1; x(1)^2 - x(1) - x(2)-8];</p><p>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</p><p>Changing the initial guess x0 = [-2;2] x0 = [5; 5] x0 = [2; 2]</p><p>Answers are </p><p>1. x =</p><p>-2.6240 1.5095 2. x =</p><p>-2.6240 1.5095 3. x =</p><p>3.5314 0.9393 4. x =</p><p>3.5314 0.9393</p><p>Problem 3</p><p>Evaluate the following double integral using MATLAB. Search for an appropriate function.</p><p>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 =</p><p>Inline function: f(x,y) = exp(-x.*y).*sin(x.*y) ans =</p><p>-0.2218</p><p>Option 2 function z = integrnd(x,y) z = (exp(-x*y).*sin(x*y));</p><p>Q = dblquad(@integrnd,-1,1,0,1) (command for command window to get output)</p><p>Problem 4</p><p>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.</p><p>Solution</p><p>%Interpolation using the 'linear' and the 'cubic' methods.</p><p>[X, Y] = meshgrid(-1:.25:1); Z = sin(X.^2 + Y.^2); figure(1) surf(X,Y,Z)</p><p>[X1, Y1] = meshgrid(-1:.05:1); Z1 = interp2(X, Y, Z, X1, Y1, 'linear interpolation'); figure(2) surf(X1, Y1, Z1), title('Bilinear interpolation')</p><p>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</p><p>Problem 5</p><p>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.</p><p>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);</p><p> figure(1) plot(t1,X) hold on plot(t2,X1,'r-') xlabel('t'); ylabel('X') title('Time History with Initial Conditions')</p><p> 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</p><p>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</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us