MAE 200B - HW5 plots

download MAE 200B - HW5 plots

of 12

Transcript of MAE 200B - HW5 plots

  • 7/24/2019 MAE 200B - HW5 plots

    1/12

    Plots

    Problem 2

    The desired convergence was reached at N=20

  • 7/24/2019 MAE 200B - HW5 plots

    2/12

  • 7/24/2019 MAE 200B - HW5 plots

    3/12

    Problem 3

    Plot for finding initial guesses for solving eigenvalues:

    Plotting function expansion for increasing number of terms N = 1, 2, 3, 4, and 5.

  • 7/24/2019 MAE 200B - HW5 plots

    4/12

  • 7/24/2019 MAE 200B - HW5 plots

    5/12

    Problem 4

  • 7/24/2019 MAE 200B - HW5 plots

    6/12

  • 7/24/2019 MAE 200B - HW5 plots

    7/12

    MATALB Source code

    PROBLEM 2

    clear all

    clc

    close all

    N=20;

    %Half-period

    L=1;

    %Definition of the function to be simulated

    syms x

    f = x -x.^2;

    %Fourier sine coefficients

    n=0;

    forj=1:N

    B(j)=2*double(int(f*sin(((2*n+1)/2)*pi*x),0, L));

    n=n+1;

    end

    %X-vector for plottingxx=(0:0.01*L:L);

    %Reconstruction of the function

    fsim=0;

    n=0;

    forj=1:N

    fsim=fsim + B(j)*sin(((2*n+1)/2)*pi*xx);

    n=n+1;

  • 7/24/2019 MAE 200B - HW5 plots

    8/12

    end

    %Plot of original and simulated function

    close

    figure('Position',[20,150,700,300],'Color','w')

    subplot('Position', [0.10,0.18,0.85,0.73])

    ezplot(f, [0,L])

    hold on

    plot(xx, fsim, 'linewidth', 1.5, 'color', 'r')

    hold off

    axis([0,L, -0.5, 0.5])

    grid on

    set(gca,'fontsize',11)

    xlabel('{\it x/L}','FontName', 'Times','FontAngle', 'normal','fontsize',16);

    ylabel('{\it f}','FontName', 'Times','FontAngle', 'normal','fontsize',16);

    legend('Original function', ['Simulated function (', num2str(N), ' terms)'], -1)

    PROBLEM 3

    clc

    clear all

    close all

    syms x

    ezplot(tan(sqrt(x)),[0 300 -15 15]) % ezplot(fun2,[xmin,xmax,ymin,ymax])

    hold on

    ezplot(-sqrt(x), [0, 300 -15 15])

    title( 'tan(x^1/2) = -x^1/2')

    % Numerical evaluation of eignevalues

    % vpasolve returns the closest, depending on the initial guess,

    % The initial guesses were changed based on the intersection points on the

    % plot

    vpasolve(tan(sqrt(x)) == -sqrt(x),x,5)

    vpasolve(tan(sqrt(x)) == -sqrt(x),x,30)

    vpasolve(tan(sqrt(x)) == -sqrt(x),x,63)

    vpasolve(tan(sqrt(x)) == -sqrt(x),x,123)

    vpasolve(tan(sqrt(x)) == -sqrt(x),x,200)

    Plotting Eigenfunctions

    figure

    ezplot(sin(sqrt(4.1.*x)),[0,1])

    x1 = 0.15

  • 7/24/2019 MAE 200B - HW5 plots

    9/12

    y1 = sin(sqrt(4.1.*x1));

    str1 = '\leftarrow X_1 = sin \lambda_1^{1/2}';

    text(x1,y1,str1)

    hold on

    ezplot(sin(sqrt(24.1.*x)),[0,1])x1 = 0.08

    y1 = sin(sqrt(24.1.*x1));

    str1 = '\leftarrow X_2 = sin \lambda_2^{1/2}';

    text(x1,y1,str1)

    hold on

    ezplot(sin(sqrt(63.7.*x)),[0,1])

    x1 = 0.1

    y1 = sin(sqrt(63.7.*x1));

    str1 = '\leftarrow X_3 = sin \lambda_3^{1/2}';

    text(x1,y1,str1)

    hold on

    ezplot(sin(sqrt(122.8.*x)),[0,1])

    x1 = 0.1y1 = sin(sqrt(122.8.*x1));

    str1 = '\leftarrow X_4 = sin \lambda_4^{1/2}';

    text(x1,y1,str1)

    hold on

    ezplot(sin(sqrt(201.9.*x)),[0,1])

    x1 = 0.1

    y1 = sin(sqrt(201.9.*x1));

    str1 = '\leftarrow X_5 = sin \lambda_5^{1/2}';

    text(x1,y1,str1)

    Plotting Fourier Sine Seriesclear all

    clc

    close all

    N = 5;

    eig = [4.1159 24.1393 63.6591 122.8892 201.8513];

    %Half-period

    L=1;

    %Definition of the function to be simulatedsyms x

    f = x-x.^2;

    %Fourier sine coefficients

    forj=1:N

    d = 1/2 - (1/(4*sqrt(eig(j))))*sin(2*sqrt(eig(j)));

    B(j)=double(int(f*sin(sqrt(eig(j))*x),0,L))/d;

  • 7/24/2019 MAE 200B - HW5 plots

    10/12

    end

    %X-vector for plotting

    xx=(0:0.01*L:L);

    %Reconstruction of the function

    fsim=0;

    forj=1:N

    fsim=fsim + B(j).*sin(sqrt(eig(j))*xx);

    end

    %Plot of original and simulated function

    close

    figure('Position',[20,150,700,300],'Color','w')

    subplot('Position', [0.10,0.18,0.85,0.73])

    ezplot(f, [0,L])

    hold on

    plot(xx, fsim, 'linewidth', 1.5, 'color', 'r')

    hold offaxis([0,L, -0.5, 0.5])

    grid on

    set(gca,'fontsize',11)

    xlabel('{\it x/L}','FontName', 'Times','FontAngle', 'normal','fontsize',16);

    ylabel('{\it f}','FontName', 'Times','FontAngle', 'normal','fontsize',16);

    legend('Original function', ['Simulated function (', num2str(N), ' terms)'], -1)

    PROBLEM 4

    clear all

    clc

    close all

    %number of eigenfunctions considered

    N=12;

    %dx increment

    dx=0.00001;

    %Evaluating Legendre polinomials

    %x interval for Legendre polinomial

    x = (-1:dx:1);

    leg0 = legendre(0,x);

    leg1 = legendre(1,x);

    leg2 = legendre(2,x);

    leg3 = legendre(3,x);

    leg4 = legendre(4,x);

    leg5 = legendre(5,x);

    leg6 = legendre(6,x);

    leg7 = legendre(7,x);

    leg8 = legendre(8,x);

    leg9 = legendre(9,x);

  • 7/24/2019 MAE 200B - HW5 plots

    11/12

    leg10 = legendre(10,x);

    leg11 = legendre(11,x);

    %Final vector, each row contains the LEgendre polinomial evaluated at x

    %interval

    leg = [leg0(1,:) ; leg1(1,:) ; leg2(1,:) ; leg3(1,:) ; leg4(1,:) ; leg5(1,:) ;

    leg6(1,:) ; leg7(1,:) ; leg8(1,:); leg9(1,:) ; leg10(1,:); leg11(1,:)];

    figure

    plot(x,leg(1,:))

    hold on

    plot(x,leg(2,:))

    hold on

    plot(x,leg(3,:))

    hold on

    plot(x,leg(4,:))

    hold on

    plot(x,leg(5,:))

    hold onplot(x,leg(6,:))

    legend('P0','P1','P2','P3', 'P4', 'P5')

    %Numerical integration X limits

    X=(-1:dx:1);

    %Function to be simulated

    fori=1:length(X)

    ifX(i)< 0

    f(i) = -1;

    else

    f(i)= 1;

    endend

    %Series expansion coefficient numerical integration versus dx

    n=0;

    forj=1:N

    c(j)=0;

    fork=1:length(X)

    fun = f(k);

    legpol = leg(j,k);

    c(j)=c(j)+dx.*fun.*legpol;

    end

    %Scaling cn by 2n+1/2

    c(j)=((2*n+1)/2)*c(j);

    n=n+1;

    end

    %Removing odd terms of c because they are supposed to be zero

    c = c(abs(c)>0.01)

    %Reconstruction of the function

    fsim=0;

  • 7/24/2019 MAE 200B - HW5 plots

    12/12

    n=0;

    forj=1:6

    fsim=fsim + c(j).*leg(2*j,:);

    n=n+1;

    end

    %Plot of original and simulated function

    close

    figure('Position',[20,150,700,300],'Color','w')

    subplot('Position', [0.10,0.18,0.85,0.73])

    plot(X,f)

    hold on

    plot(X, fsim, 'linewidth', 1.5, 'color', 'r')

    hold off

    axis ([-1 1 -3 3])

    grid on

    set(gca,'fontsize',11)

    xlabel('{\it x/L}','FontName', 'Times','FontAngle', 'normal','fontsize',16);

    ylabel('{\it f}','FontName', 'Times','FontAngle', 'normal','fontsize',16);legend('Original function', ['Simulated function (', num2str(j), ' terms)'], -1)

    Published with MATLAB R2015a

    http://www.mathworks.com/products/matlab/http://www.mathworks.com/products/matlab/http://www.mathworks.com/products/matlab/