You are on page 1of 9

Exercises 1: MAE Unit 1

Name: Toni Forcada.


MATLAB release used: 2017b
Collaboration Info:

Exercise 1. Plot of a damped sinusoid.

Explanation: Explain here how you solved the exercises.


I created two functions f(x) and g(x) and I used Plot function to plot the two graphics.
MATLAB Code:
% (1) plot(x,f,x,g)
title('2-D Line Plot')
x=0:0.01:10; xlabel('x')
f=exp(-x/2).*sin(4*pi*x); ylabel('f(x)*g(x)')

% (2) % (3)

g=cos(6*pi*x).*x.*x.*exp(-x); subplot(2,1,2)
plot(f,g)
xlabel('f(x)')
figure ylabel('g(x)')
subplot(2,1,1)

Results:

Comments:
No problems. I had to pay attention in vector direction to execute products.

1
Exercise 2. Polygons and colours.

Explanation: Explain here how you solved the exercises.


E.g., I used fill and polyshape in order to create different polygons.

MATLAB Code:

dim=[20 13];
colum=dim(:,1);
files=dim(:,2); %figure3 |
figure function [] = figure_3(m,n,r)
hold on
for j=1:colum-1
for k=1:files-1 y3=[n n n+4 n+4];
x3=[m m+1 m+1 m];
f3=rotate(polyshape(x3,y3),r,[m
xh=[k k k+1 k+1]; n]);
xv=[j j+1 j+1 j]; plot(f3)
fill(xh,xv,[0.5 0.5 end
0.5])
end
end %figure4 Z

figure_1(9,4,0); function [] = figure_4(m,n,r)


figure_1(6,7,180); y4=[n n n+1 n+1 n+3 n+3 n+2 n+2
figure_1(4,4,0); n+1];
figure_2(5,1,90); x4=[m m+1 m+1 m+2 m+2 m+1 m+1 m
figure_2(4,5,180); m];
figure_2(6,1,0); f4=rotate(polyshape(x4,y4),r,[m
figure_3(4,13,0); n]);
figure_3(7,2,0); plot(f4)
figure_4(1,1,0);
figure_4(11,1,0); end
figure_5(8,1,0);
figure_6(10,1,0);
hold off %figure5 Cuadrat
%figure1 .:. function [] = figure_5(m,n,r)
function [] = figure_1(m,n,r) y5=[n n n+2 n+2];
y1=[n n n n n+1 n+1 n+2 n+2 n+1 x5=[m m+2 m+2 m];
n+1 n]; f5=rotate(polyshape(x5,y5),r,[m
x1=[m m+1 m+2 m+3 m+3 m+2 m+2 n]);
m+1 m+1 m m]; plot(f5)
f1=rotate(polyshape(x1,y1),r,[m end
n]);
plot(f1)
end %figure6 L inv
%figure2 L function [] = figure_6(m,n,r)
function [] = figure_2(m,n,r) y6=[n n n+1 n+2 n+2 n+3 n+3 n];
y2=[n n n n+1 n+1 n+3 n+3 n]; x6=[m m+1 m+1 m+1 m+2 m+2 m m];
x2=[m m+1 m+2 m+2 m+1 m+1 m m]; f6=rotate(polyshape(x6,y6),r,[m
f2=rotate(polyshape(x2,y2),r,[m n]);
n]); plot(f6)
plot(f2) end
end

Results:

2
Comments:
Due different troubles creating polygons with fill commands I used polyshape instead. Because
of this decision color of each polygon is not the appropriate one.

Exercise 3. Distorted plane.

Explanation:

I used meshgrid and surf to plot these two functions.

MATLAB Code:

x=-10:1:10;
y=-10:1:10;
z=-10:1:10;
K=1;
R=2;
[X,Y] = meshgrid(x);
F = -K*exp((-X.^2-Y.^2)/(5*R*R));

[A,B,C]=sphere(20);
figure
hold on
surf(X,Y,F/R)
surf(R*A,R*B,C/R)
hold off

3
Results:
.

Comments:
E.g., I had some problems with the Z axis. But it is only a representation issue.

Exercise 4. Sonine polynominals

Explanation: Explain here how you solved the exercises.


E.g., I used linspace to build a vector t, and the standard mathematical functions sin and
exp to build another vector x holding the sinusoid data. Then I called plot. I have selected a
time range large enough to hold several cycles of the sinusoid.

MATLAB Code:

t=-2:0.1:10;
s{1}=ones(1,length(t));
alpha=1.5;
s{2}=1+alpha-t;

for k=3:6

s1=s{k-1}.*(2*k-1+alpha-t)*1/k;
s2=s{k-2}.*(k-1+alpha)*1/k;
s{k}=s1-s2;
end

figure
hold on
for k=1:6

plot(t,s{k})
end
axis([t(1) t(length(t)) -10 10])

hold off

4
Results:

Comments:
I have developed code and we achieve the desired plot.

5
Exercises 1: MAE Unit 2
Name: Jose Antonio Forcada Sans
MATLAB release used: 2017b
Collaboration Info:

Exercise 1.
Explanation:

I analized different vectorial product methods taking account computational time.

MATLAB Code:

Execute code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% A=zeros(N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% for kr=1:N,
%%%%%% for kc=1:N,
% MAE Exercise 1, problem set
3 A(kr,kc)=B(kr,:)*C(:,kc);
% ETSETB-UPC end
%%%%%%%%555555555555555555555 end
55555555555555555555555555555 toc
555555
clear clear
tic tic
N=2000; N=2000;
B=randn(N); B=randn(N);
C=randn(N); C=randn(N);
for kr=1:N, A=zeros(N);
for kc=1:N, for kc=1:N,
A(:,kc)=B*C(:,kc);
A(kr,kc)=B(kr,:)*C(:,kc); end
end toc
end
toc clear
tic
clear N=2000;
tic B=randn(N);
N=2000; C=randn(N);
B=randn(N); A=B*C;
C=randn(N); toc

Results:

Elapsed time is 97.198796 seconds.


Elapsed time is 93.656629 seconds.
Elapsed time is 3.392039 seconds.
Elapsed time is 0.311829 seconds.

1
Comments:

1st method: We calculate the result using 2000*2000 products without meory pre-allocation.
(A1 variable has not be declared. Size of matrix change every step)

2n Method: Same as previous but with memory pre-allocation.

3r Method: We mutiply each colom with all matrix.

4th Method: Vectorial product of two same dimensions matrix can be computed in matlab
using simple multiplication method.

Exercise 2. Linear Prediction


Explanation:

MATLAB Code:

clear

load ex2_data.mat
[resultx1,resulty1]=lin_interp(xknown1,yknown1,xunknown);

figure('Name','result1')
hold on
plot(xknown1,yknown1,'bo')
plot(resultx1(find(resulty1)),resulty1(find(resulty1)),'r*')
hold off

[resultx2,resulty2]=lin_interp(xknown2,yknown2,xunknown);

figure('Name','result2')
hold on
plot(xknown2,yknown2,'bo')
plot(resultx2(find(resulty2)),resulty2(find(resulty2)),'r*')
hold off

[resultx3,resulty3]=lin_interp(xknown3,yknown3,xunknown);

figure('Name','result3')
hold on
plot(xknown3,yknown3,'bo')
plot(resultx3(find(resulty3)),resulty3(find(resulty3)),'r*')
hold off

function [resultx, resulty] = lin_interp(xdata, ydata, x)


resultx=sort(unique([xdata x]));

2
resulty=zeros(1,length(resultx));
for k= 1:length(x)
xin=x(k);

if (xin>xdata(1) || xin<xdata(length(xdata)-1))
xa=xdata(find(xdata>xin,1,'first'));

xb=xdata(find(xdata<xin,1,'last'));
ya=ydata(find(xdata==xa));
yb=ydata(find(xdata==xb));
resulty(find(resultx==xin))= yb + (ya - yb)*((xb-xin)/(xb-
xa));
else
error('Error. \nValue of X is not between Xdata values.')

end
end
end

Results:

I achieved desired results:

Comments:

Results are good and did interpolation correctly as can be sawn at images.

3
Exercise 3.
Explanation:

In this exercise I have to implement Taylor Series Approximation for different functions.
MATLAB Code:

Results:

Comments:

Exercise 4.
Explanation:

MATLAB Code:

Results:

Comments:

You might also like