Sei sulla pagina 1di 12

Plots

Problem 2
The desired convergence was reached at N=20

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.

Problem 4

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;
for j=1:N
B(j)=2*double(int(f*sin(((2*n+1)/2)*pi*x),0, L));
n=n+1;
end
%X-vector for plotting
xx=(0:0.01*L:L);
%Reconstruction of the function
fsim=0;
n=0;
for j=1:N
fsim=fsim + B(j)*sin(((2*n+1)/2)*pi*xx);
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])
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

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.1
y1 = 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 Series


clear 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 simulated
syms x
f = x-x.^2;
%Fourier sine coefficients
for j=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;

end
%X-vector for plotting
xx=(0:0.01*L:L);
%Reconstruction of the function
fsim=0;
for j=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 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 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
leg1
leg2
leg3
leg4
leg5
leg6
leg7
leg8
leg9

=
=
=
=
=
=
=
=
=
=

legendre(0,x);
legendre(1,x);
legendre(2,x);
legendre(3,x);
legendre(4,x);
legendre(5,x);
legendre(6,x);
legendre(7,x);
legendre(8,x);
legendre(9,x);

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 on
plot(x,leg(6,:))
legend('P0','P1','P2','P3', 'P4', 'P5')
%Numerical integration X limits
X=(-1:dx:1);
%Function to be simulated
for i=1:length(X)
if X(i)< 0
f(i) = -1;
else
f(i)= 1;
end
end
%Series expansion coefficient numerical integration versus dx
n=0;
for j=1:N
c(j)=0;
for k=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;

n=0;
for j=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

Potrebbero piacerti anche