You are on page 1of 8

# MATLAB Solution

Assignment 1 (Solution)
1. (a)
Matlab Code Input:
lc=3;
lp=3;
W=100;
d=0.3:2.5:lp;
% Calculate tension
T=W *lc *lp./(d.*sqrt(lp^2-d.^2));
plot(d,T,'-p');
xlabel('Distance d in m');
ylabel('Tension in string in N');
grid on;
[Tmin, I]=min(T);
fprintf('Minimum tension is %g N at %g cm',Tmin,d(I))
Output:

## (b) Pole Length lp vs Distance d

2. (a)
x0=1; xt=2; xstep=80; dx=1/(xstep-1);
y0=0; yt=2-0.5*x; ystep=80; dy=1/(ystep-1);
sum2=0;
for i1=1:ystep+1
sum1=0;
y=(i1-1)*dy;
x2step=floor(y/dx);
for i=1:xstep+1
if i<=x2step
sum1=sum1+dx*sqrt(x+y);
end
x=(i-1)*dx;
end
sum2=sum2+sum1;
end
sum3=sum2*dy

(b)
INTfxy=int2s(f,1,2,0,2-0.5*x,50,50)
f=sqrt(x+y);
c=0;
N=50;
N=50;
d=2-0.5*x;
a=1;
b=2;
if ceil(M)=floor(M)
hx=M;
M=ceil((b-a)/hx;
end
if mod(M,2)=0,M=M+1;end
hx=(b-a)/M;
m=1:M+1;
x=a+(m-1)*hx;
if isnumeric(c),cx(m)=c;
else cx(m)=feval(c,x(m));
end
if isnumeric(d),dx(m)=d;
else dx(m)=feval(d,x(m));
end
if ceil(N)=floor(N)
hy=N;
Nx(m)=ceil((dx(m)-cx(m))/hy);
ind=find(mod(Nx(m),2)=0);
Nx(ind)=Nx(ind)+1;
else
if mod(N,2)=0,N=N+1;
end
for m=1:M+1
sx(m)=smpsns_fxy(f,x(m),cx(m),dx(m),Nx(m));
end
kodd=2:2:M;
keven=3:2:M-1;
INTfxy=hx/3*(sx(1)+sx(M+1)+4*sum(sx(kodd))+2*sum(sx(keven)));
3. (a)
T = [0 4 8 12 16 20 24 ];
v = [1.7923 1.5676 1.3874 1.2396 1.1168 1.0105 0.9168 ];
y =A+b*T;
y=log(v);
A=log(a);

plot( v,T)
xlabel('Viscosity,cm^2*sec^-1'),ylabel('Temperature, degrees C')
Autometic generated M-Code
function createfigure(X1, Y1)
%CREATEFIGURE(X1,Y1)
% X1: vector of x data
% Y1: vector of y data
%

## Auto-generated by MATLAB on 05-Sep-2010 22:39:49

% Create figure
figure1 = figure;
% Create axes
axes('Parent',figure1);
box('on');
hold('all');
% Create plot
plot(X1,Y1);
% Create xlabel
xlabel('Viscosity,cm^2*sec^-1');
% Create ylabel
ylabel('Temperature, degrees C');
Plot Temperature vs Viscosity

Curve fitting

(b)
T = [0 4 8 12 16 20 24 ];
v = [1.7923 1.5676 1.3874 1.2396 1.1168 1.0105 0.9168 ];
a = polyfit(v,T,1)
That = polyval(a,v);
err = That - T
MSE = mean(err.^2)
RMSE = sqrt(MSE)
plot(v,T,'o',v,That),title('Linear Regression');
xlabel('Viscosity,cm^2*s^-1'),ylabel('Temperature, degrees C');
grid,axis([0,2,0,30]),legend('measured','estimated',4)
Output:
a =
-27.2511

47.1578

err =
-1.6843

0.4390

1.3496

1.3773

MSE =
1.5358
RMSE =
1.2393

## Bets fit will be

y =-27.2511*T - 1.6843

0.7238

-0.3794

-1.8260

Plot

Curve fit