Professional Documents
Culture Documents
% (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.
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
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.
Explanation:
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.
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:
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:
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)
4th Method: Vectorial product of two same dimensions matrix can be computed in matlab
using simple multiplication method.
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
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:
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: