You are on page 1of 48

Success isnt a matter of being the best & winning the

race. Success is a matter of handling the worst & still


finishing the race.

As you know I am a student of this faculty and its my first course in


MATLAB.
I have no permission to authorship a book due to my low educational
degree. With respect to these points, please do not consider this present as
educational textbook. I take no responsibility against wrong solution (if any) of
problems.

Hamed Parsa
Junior student of
E.E.E. department
March 2010
Email: hamede66@yahoo.com

Chapter 1

>> r=2.5;
>> I=(pi/8-8/(9*pi))*r^4

>> c=5;
>> k=(4*c-1)/(4*c-4)+0.615/c

>> B=0.6;
>> K=3/(1-B)^3*(0.5-2*B+B*(1.5-log(B)))

Homework Problems not solved.

>> A=1.7;B=1.2;
>> D=1.265*((A*B)^3/(A+B))^(1/5)

>> n=6;M=1/sin(pi/n);h=(1+M^2)/(4*M);
>> alpha=acos(sqrt(h^2+2)-h);
>> answer=M*(1-M^2)*sin(alpha)/(1+M^2-2*M*cos(alpha))^2

>> L=3000;d=45;V=1600;
>> p=0.03*L/d^1.24*(V/1000)^1.84

>> v=0.3;E=3e+7;d1=1.5;d2=2.75;F=100;z =0.01


>> a=(3*F/8*2*((1-v^2)/E)/(1/d1+1/d2))^(1/3);
>> pmax=3*F/(2*pi*a^2);
>> Qz=-pmax/(1+z^2/a^2)
>> Qx=-pmax*((1-z/a*atan(a/z))*(1-v)-0.5*(1+z^2/a^2)^-1)

>> v=0.3;E=3e+7;d1=1.5;d2=2.75;F=100;L=2;z=0.001;
>> b=sqrt(2*F/(pi*L)*2*((1-v^2)/E)/(1/d1+1/d2));h=(1+z^2/b^2);
>> pmax=2*F/(pi*b*L);
>> Qz=-pmax/sqrt(h);
>> Qy=-pmax*((2-1/h)*sqrt(h)-2*z/b);
>> Qx=-2*v*pmax*(sqrt(h)-z/b);
>> Tyz=0.5*(Qy-Qz);

>> e=0.8;a=(1-e^2);
>> NL=pi*e*sqrt(pi^2*(a)+16*e^2)/a^2

>> h=1.25; d0=0.25; d1=0.625; E=3e+7; d2=d1+h*tan(pi/6);


>> y=log((d2-d0)*(d1+d0)/((d2+d0)*(d1-d0)));
>> k=pi*E*d0*tan(pi/6)/y

>> alpha=1.2e-5;E=3e+7;v=0.3;Ta=500;Tb=300;a=0.25;b=0.5;r=0.375;
>> Tc=Ta-Tb; c=log(b/a); d=log(b/r); k=a^2/(b^2-a^2);
>> T=Tb+Tc*d/c; h=alpha*E*Tc/(2*(1-v)*c);
>> Qt=h*(1-k*(b^2/r^2+1)*c-d);
>> Qr=h*(k*(b^2/r^2-1)*c-d);

>> p=0.3;k=1.4; % pe/po=p


>> Y=sqrt(k/(k-1)*(p^(2/k)-p^((k+1)/k)))

>> x=0.45;y=sqrt(16*x^2+1);
>> K=1.2/x*(y+1/(4*x)*log(y+4*x))^(-2/3)

>> n1=0; n2=1;


>>Pi1=(sqrt(8)/9801*gamma(4*n1+1)*(1103+26390*n1)/(gamma(n1+1)^
4*396^(4*n1)))^(-1);
>>Pi2=(1/Pi1+(sqrt(8)/9801*gamma(4*n2+1)*(1103+26390*n2)/(gamma
(n2+1)^4*396^(4*n2))))^-1;
>> % These two statement cannot be broken up as shown. They have been
presented in two lines because of page width restrictions.

>> pi-Pi1 %show the approximation of pi with a different less than 10^-7
ans =
-7.64235e-008
>> pi-Pi2 %show the approximatoin of pi with a different less than 10^-15
ans =
-4.44089e-016

>> k=1.4; r=10; rc=3;


>> eta=1-1/r^(k-1)*(rc^k-1)/(k*(rc-1))

>> k=1.4 ; M=2 ; %A/A*=X


>> X=1/M*(2/(k+1)*(1+(k-1)/2*M^2))^((k+1)/(2*(k-1)))

Chapter 2

>> a=-1:2:13;b=1:2:15;
>>a+b;
%part a)
>>a-b;
%part b)
>>a'*b ; det(a'*b); %part c)
>>a*b';
%part d)

>> x=[17 -3 -47 5 29 -37 51 -7 19]; a=sort(x);


>> b=sort(a(1:4),'descend');c=sort(a(5:9),'descend'); y=[b c];
>> %we can use fliplr function too.
>> b=fliplr(a(1:4));c=fliplr(a(5:9)); y=[b c];

>> y=[0 -0.2 0.4 -0.6 0.8 -1.0 -1.2 -1.4 1.6];
>> z=sin(y);h=sort(z)
h=
-0.985 -0.932 -0.841 -0.564 -0.198 0 0.389 0.717 0.999
>> a = min(h(1:5)); b = max(h(1:5)); %part a)
>> c=sqrt(h(6:9));
%part b)

>> x=logspace( log10(6) , log10(106) , 8);


>> y=x(5);
%part a)
>> h=[x(1) x(3) x(5) x(7)]; %part b)

>> z=magic(5);
>> z(: , 2)=z(: , 2)/sqrt(3);
>> z(5 , :)=z(3 , :) + z(5 , :);
>> z(: , 1)=z(: , 1) .* z(: , 4);
>> q=z-diag(diag(z))+diag([2 2 2 2 2]);
>> diag(q*q');
>> c=q.^2;
>> max(max(c)); min(min(c));

% part a) I
% part a) II
% part a)III
% part a) IV
% part b)
% part c)
% part d)

>> w=magic(2);
>> a=repmat(w, 2, 2);
% part a)
% part b)
>> b=repmat(w, 3, 1);
>> c=repmat([w w'], 3 ,1);
% part c)
>> da=[w w ;w w];db=[w ;w ;w];dc=[w w';w w';w w']; % part d)

>> x=magic(3);
>> new=[x(2,:) ; x(3,:) ; x(1,:)];
>> new=[x(:,3) , x(:,1) , x(:,2)];

% part a
% part b

>> a=1;b=1.5;e=0.3;phi=0:360;
>> s=a*cosd(phi)+sqrt(b^2-(a*sind(phi)-e).^2);
>> plot(phi,s)

>> t=1/sqrt(19);Pt=4.3589;n=linspace(2,25,300);
>> Po=1+2*cumsum(sin(n*pi*t).^2./(n*pi*t).^2);
>> plot(n,Po)

%to/T=t

>> x=1:0.5:5;
>> n=1:100;
>> [xx,nn]=meshgrid(x,n);
>> sn=prod(1-xx.^2./(nn.^2-2.8));
>> sj=a./(sin(pi*a)*sqrt(a^2+x.^2)).*sin(pi*sqrt(a^2+x.^2));
>> en=100*(sn-sj)./sj;

>> x=[72 82 97 113 117 126 127 127139154 159 199 207];
>> B=3.644;
>> Delta=(1/length(x))*sum(x.^B)
Delta =
3.0946e+028

>> phi=linspace(0,90,10);theta=linspace(0,360,24);b=2;
>> [theta2,phi2]=meshgrid(theta,phi);
>> x=b*sind(phi2).*cosd(theta2);
>> y=b*sind(phi2).*sind(theta2);
>> z=b*cosd(phi2);
>> mesh(x,y,z);
>>% The result after using the rotate icon in the figure window.

>> x=linspace(0.1,1,5); y=pi*x*sqrt(2); n=0:25;


>> t=2*pi^4./y.^3.*(sinh(y)+sin(y))./(cosh(y)-cos(y));
>> [xx,nn]=meshgrid(x,n);
>> S= sum(1./(nn.^4+xx.^4));
>> compare=[t' S' (t-S)'];

>> x=[1 2 3 4 5 6]; k=0:25;n=2;


>> [xx,kk]=meshgrid(x,k);
>> p=sum((-1).^kk.*(xx/2).^(2.*kk+n)./(gamma(kk+1).*gamma(kk+n+1)));
>> besselj(2,x);
>> r=besselj(2,x);
>> compare=[p' r']

>> k=1:13;n=7;
>> s=sum(cos(k*pi/n))

>> w=0.5*[-1 -1; 1 -1;-1 1; 1 1]; q=0.5*[1 -1 -1 1; 1 1 -1 -1; 1 -1 1 -1; 1 1 1 1];


>> I1=w'*w; I2=q'*q;

>>e=pi/6;a1=1;a2=2; a3=3;
>>A1=[cos(e) -sin(e) 0 a1*cos(e);sin(e) cos(e) 0 a1*sin(e);0 0 1 0;0 0 0 1];
>>A2=[cos(e) -sin(e) 0 a2*cos(e);sin(e) cos(e) 0 a2*sin(e);0 0 1 0;0 0 0 1];
>>A3=[cos(e) -sin(e) 0 a3*cos(e);sin(e) cos(e) 0 a3*sin(e);0 0 1 0;0 0 0 1];
>>T3=A1*A2*A3;
>>qx=T3(1,4);qy=T3(2,4);
>>%For finding orientation with respect to Fig2.8 orientation of X3 is
%arctan(uy/ux) and orientation of Y3 is arctan(vy/vx)
>>X3=atand(t(2,1)/t(1,1))
X3 =
90.0
>>Y3=atand(t(2,2)/t(1,2))
Y3 =
0.00
>>%They are parallel to Y0 and X0 axes respectively.

>> X= [17 31 5; 6 5 4; 19 28 9; 12 11 10];

>> H=X*inv(X'*X)*X';
>> diag(H)

Part a)
>> n=1:2:399; t=linspace(-0.5,0.5,200);
>> u= sin(2*pi*n'*t);
>> fa=4/pi*cumsum(1./n*u);
>> plot(t,fa)

part b)
>> n= 1:200; t=linspace(-1,1,200);
>> u= sin(2*pi*n'*t);
>> fb=1/2+1/pi*cumsum(1./n*u);
>> plot(t,fb)

part c)
>> n= 1:200; t=linspace(-1,1,200);
>> u= sin(2*pi*n'*t);
>> fb=1/2-1/pi*cumsum(1./n*u);
>> plot(t,fb)

part d)
>> n= 1:200; t=linspace(-1,1,200);
>> u= cos((2*n-1)'*pi*t);
>> fd=pi/2-4/pi*cumsum(1./(2*n-1).^2*u);
>> plot(t,fd)

part e)
>> n= 1:200; t=linspace(-1,1,200);
>> u= cos(2*n'*pi*t);
>> fe=2/pi+4/pi*cumsum(1./(1-4*n.^2)*u);
>> plot(t,fe)

part f)
>> n=2:2:106;t=linspace(-2,2,200);
>> ff=1/pi+1/2* sin(pi*t)-2/pi*cumsum(1./(n.^2-1)*cos(pi*n'*t));
>> plot(t,ff)

part g)
>> n=1:250;t=linspace(0,4*pi,350);
>> u1=1./(1+n.^2)*cos(n'*t);
>> u2=n./(1+n.^2)*sin(n'*t);
>> fg=(exp(2*pi)-1)/pi*(1/2+cumsum(u1-u2));
>> plot(t,fg)

part h)
>> a=0.25;n=1:2:105;t=linspace(2,-2,200);
>> fh=4/a^2*cumsum(sin(n*pi*a)./(pi*n).^2*sin(n'*pi*t));
>> plot(t,fh)

>> n=1:25;a=sqrt(3);theta=(10:10:80)*pi/180;
>> S1n=1./(n.^2+a^2)*cos(n'*e);
>> S1inf=pi*cosh(a*(pi-e))/(2*a*sinh(pi*a))-1/(2*a^2);
>> S2n=n./(n.^2+a^2)*sin(n'*e);
>> S2inf=pi*sinh(a*(pi-e))/(2*sinh(pi*a));
>> e1=100*((S1n-S1inf)./S1inf)
>> e2=100*((S2n-S2inf)./S2inf)

>> eta=0:1/14:1;E=0:1/14:1;n=1:2:length(E);a=2;
>> u1=1./(n.*sinh(n*pi*2)); u2=sinh(n'*eta*pi*2); u3=sin(pi*n'*E);
>> [eta1,u11]=meshgrid(eta,u1);
>> T=(u11.*u2)'*u3;
>> mesh(E,eta,T)

>> n=1:50;a=0.25;t=0:0.05:2;eta=0:0.05:1;
>> u1=sin(n*pi*a)./n.^2;u2=sin(n'*pi*eta);u3=cos(n'*pi*t);
>> [eta1,u11]=meshgrid(eta,u1);
>> c=2/(a*pi*(1-a));
>> u=c*(u11.*u2)'*u3;
>> mesh(t,eta,u)
>>% The result after using the rotate icon in the figure window.

Homework problems not solved.

>> v=0.4;E1=3e+5;E2=3.5e+4;Uo=0.01;
>> a=0.192;b=0.25;c=0.312;t=1+v;h=1-v;
>> S=[1
a^2
0
0
1
b^2
-1
-b^2
-t
h*b^2
t*E1/E2
-h*b^2*E1/E2
0
0
-t
h*c^2]
S=
1.0000 0.0369
0
0
1.0000 0.0625 -1.0000 -0.0625
-1.4000 0.0375 12.0000 -0.3214
0
0 -1.4000 0.0584
>> y=[0 0 0 -Uo*E2*c];
>> w=inv(S)*y';x=w';
>> A1=x(1);B1=x(2);A2=x(3);B2=x(4);
>> Qee1=-A1/b^2+B1;
>> Qee2=-A2/b^2+B2;

Chapter 3

>>a=(1+sqrt(5))/2;b=(1-sqrt(5))/2;c=1/sqrt(5);
>>n=0:15;
>>Fn=c*(a.^n-b.^n);
>>disp([repmat('F',16,1) num2str(n') repmat(' = ',16,1) num2str(Fn') ]);
>>fprintf(1,'F %2.0f = %3.0f\n ',[n ;Fn]);

% To go next line without execution use shift+Enter


>>ft=input('Enter the value of length in feet :');
disp([num2str(ft) ' ft = ' num2str(ft*0.3048) ' m'])

Homework Problem not solved.

>>D=input('Enter a positive integer < 4.5x10^15 : '); % shift+Enter


disp(['The binary representation of ' num2str(D) ' is ' dec2bin(D)] )

z=input('Enter the complex number:');


% shift+Enter
disp(['The magnitude and phase of ' num2str(z) ' is']); % shift+Enter
disp(['Magnitude = ' num2str(abs(z)) '
phase angle = '
num2str(angle(z)*180/pi) ' degrees']);
>>%The last disp statement cannot be broken up as shown. It has been
presented in two lines because of page width restrictions.

Chapter 4

function y=myexp1(h,a,b)
for n=1:length(h);
if h(n)>a && h(n)<b;
h(n)=1;
else
h(n)=0;
end
end
disp([h])
end

function myexp2(x)
k=0;
for m=2:length(x)
xn=1/m*sum(x(1:m));
Sn2=1/(m-1)*(sum(x(1:m).^2)-m*(xn).^2);
k=k+1;
y(k)=Sn2;
end
disp(['Sn2 = [' num2str(y) ' ]'])
end

function myexp3(a,b)
k=1; c=0.5*(b+a/b);
%where c=b(n+1)
while abs(b-c)>1e-6
k=k+1;
b=0.5*(b+a/b);
c=0.5*(b+a/b);
end
disp(['n-iteration=' num2str(k)])

end

part a)*
function y=myexp4(x0)
x0=0:200;
r=x0.^2+0.25;
x=0:5:200;
for t=1:(length(x)-1);
y(t)= r(5*t);
end
y=[r(1) y];
plot(x,y,'ks')
end
part b)
function y=myexp5(x0)
x0=0:200;
r=x0.^2+0.25;
x=0:5:200;y=[];k=0;
while (length(x)-1)~=length(y)
k=k+1;
y(k)= r(5*k);
end
y=[r(1) y];
plot(x,y,'ks')
end

function chisquare(x,e)
k=0;e=[e 5];x=[x 0];
for n=1:length(e)
if e(n)<5
e(n:n+1)=cumsum(e(n:n+1));

x(n:n+1)=cumsum(x(n:n+1));
else
k=k+1;
p(k)=n;
end ;end
%e modified= u and x modified=h
h=x(p);u=e(p);
if (u(end)-5)<5
u(end)=u(end)-5+u(length(u)-1);
u(length(u)-1)=[];
h(end)= h(length(h)-1)+h(end);
h(length(h)-1)=[];
end
y=sum((h-u).^2./u);
disp(['e modified = [' num2str(u) '], x modified = [' num2str(h) '], X2='
num2str(y) ])
end
>>%Thist disp statement cannot be broken up as shown. It has been
presented in two lines because of page width restrictions.

function myexp6(p,s)
a=length(p) ;b=length(s);
if a>=b
t=s+p(a-b+1:a);
h=[p(1:(a-b)) t]
elseif a<b
t=p+s(b-a+1:b);
h=[s(1:b-a) t]
end
end

Homework problems not solved.

Chapter 5

>>h=[6 3 2 1.5 1.2 1.1 1.07 1.05 1.03 1.01]; %h=D/d


>>a=[0.33 0.31 0.29 0.26 0.22 0.24 0.21 0.2 0.18 0.17];
>>c=[0.88 0.89 0.91 0.94 0.97 0.95 0.98 0.98 0.98 0.92];
>>%Fifth order polynomial
>>aa=polyfit(h,a,5);cc=polyfit(h,c,5);
>>%Obtain Orginal Value Of Kt
>>kt=c.*(h/2-0.5).^(-a);
>>%Compare
>>k1=polyval(cc,h).*(h/2-0.5).^(-polyval(aa,h));
>>k1-k
ans =
Columns 1 through 6
-1.0971e-008 -1.8084e-005 8.9483e-4 -0.011004

0.087181

-0.09516

Columns 7 through 10
-0.059276 -0.060901 0.016685 0.16796
>>%Fit data with a spline
>> n=linspace(1.01,6,100);
>> aa=spline(h,a,n);
>> cc=spline(h,c,n);
>> k=0;
>> for h=[6 3 2 1.5 1.2 1.1 1.07 1.05 1.03 1.01]
k=k+1;
a2(k)=interp1(n,aa,h);
c2(k)=interp1(n,cc,h);
end
>>k2=c2.*(h/2-0.5).^(-a2)
>>%Compare
>>k2-k
>> -kt+kt2
ans =
0 0.0005 0.0009 -0.0062 0.0101 -0.0271 0.0321 -0.0637 -0.0408 0

>>%2nd method (spline) is better.

>> Qx=100;Qy=-60;Qz=80;Txy=-40;Tyz=50;Tzx=70;
>> C0=Qx*Qy*Qz+2*Txy*Tyz*Tzx-Qx*Tyz^2-Qy*Tzx^2-Qz*Txy^2;
>> C1=Txy^2+Tyz^2+Tzx^2-Qx*Qy-Qy*Qz-Qz*Qx;
>> C2=Qx+Qy+Qz;
>> disp([C0 C1 C2])
-844000
11800
120
>> F=inline('Q.^3-120*Q.^2-11800*Q-(-844000)','Q');
>> x=linspace(-100,200,500);
>> plot(x,F(x));
>> Q1=fzero(F,170);Q2=fzero(F,50); Q3=fzero(F,-100);
>> T12=(Q1-Q2)/2;T23=(Q2-Q3)/2;T13=(Q1-Q3)/2;

>> F=inline('tan(x)-x','x');
>> x=linspace(0,15,5000);
>> plot(x,F(x),'k',[0 15],[0 0],'r')
>> axis([0 15 -5 5])
>> r1=fzero(F,[-1 1]);r2=fzero(F,[4.2 4.6]);r3=fzero(F,[7.6 7.8]);
>> r4=fzero(F,[10.85 10.95]);r5=fzero(F,[14 14.1]);
>> disp([r1 r2 r3 r4 r5]);

>> %Part a
>> H=inline('2*cot(x)-10*x+0.1./x','x');
>> x=linspace(0,15,5000);
>> plot(x,H(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -4 4])
>> r1=fzero(H,[0.4 0.6]);r2=fzero(H,[3.2 3.3]);r3=fzero(H,[6.3 6.4]);
>> r4=fzero(H,[9.44 9.46]);r5=fzero(H,[12.57 12.6]);
>> disp([r1 r2 r3 r4 r5]);
>> %Part b
>> H=inline('2*cot(x)-x+1./x','x');
>> x=linspace(0,15,5000);

>> plot(x,H(x),'k',[0 15],[0 0],'r');


>> axis([0 15 -4 4])
>> r1=fzero(H,[1 2]);r2=fzero(H,[3.5 4]);r3=fzero(H,[6.5 7]);
>> r4=fzero(H,[9.5 10]);r5=fzero(H,[12.6 13]);
>> disp([r1 r2 r3 r4 r5]);

>> G=inline('besselj(0,x).*bessely(0,2*x)besselj(0,2*x).*bessely(0,x)','x');
>> x=linspace(0,20,5000);
>> plot(x,G(x),'k',[0 20],[0 0],'r');
>> r1=fzero(G,[2 4]);r2=fzero(G,[6,8]);r3=fzero(G,[8 10]);
>> r4=fzero(G,[12 14]);r5=fzero(G,[14 16]);
>> disp([r1 r2 r3 r4 r5]);

>> %Mo/mo= m
>> % Case 1
>> H=inline('0*x.*(cos(x).*sinh(x)sin(x).*cosh(x))+cos(x).*cosh(x)+1','x');
>> x=linspace(0,20,5000);
>> plot(x,H(x),'k',[0 20],[0 0],'r');;
>> axis([0 20 -20 20])
>> r1=fzero(H,[1 4]);r2=fzero(H,[4,6]);r3=fzero(H,[6 8]);
>> r4=fzero(H,[10 12]);r5=fzero(H,[13 16]);
>> disp([r1 r2 r3 r4 r5])
>> % Case 2
>> H=inline('0.2*x.*(cos(x).*sinh(x)sin(x).*cosh(x))+cos(x).*cosh(x)+1','x');

>> plot(x,H(x),'k',[0 20],[0 0],'r');


>> axis([0 20 -20 20])
>> r1=fzero(H,[1 3]);r2=fzero(H,[3,5]);r3=fzero(H,[6 8]);
>> r4=fzero(H,[10 12]);r5=fzero(H,[12 14]);
>> disp([r1 r2 r3 r4 r5])
>> % Case 3
>> H=inline('x.*(cos(x).*sinh(x)-sin(x).*cosh(x))+cos(x).*cosh(x)+1','x');
>> plot(x,H(x),'k',[0 20],[0 0],'r');
>> axis([0 20 -20 20])

>> r1=fzero(H,[1 3]);r2=fzero(H,[3,5]);r3=fzero(H,[6 8]);


>> r4=fzero(H,[10 12]);r5=fzero(H,[12 14]);
>> disp([r1 r2 r3 r4 r5])

>>
>>G=inline('tanh(x)-tan(x)','x');
>> x=linspace(0,15,5000);
>> plot(x,G(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -1 1])
>> r1=fzero(G,[-0.5 0.5]);r2=fzero(G,[3.5 4.5]);r3=fzero(G,[6.5 7.5]);
>> r4=fzero(G,[9 10.5]);r5=fzero(G,[13 14]);
>> disp([r1 r2 r3 r4 r5])

>> % Case 1
>> G=inline('besselj(0,x).*besseli(1,x)+besseli(0,x).*besselj(1,x)','x');
>> x=linspace(0,15,5000);
>> plot(x,G(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -22 22])
>> r1=fzero(G,[0 1]);r2=fzero(G,[2 4]);r3=fzero(G,[5 7]);
>> disp([r1 r2 r3])
>> % Case 2
>> G=inline('besselj(1,x).*besseli(2,x)+besseli(1,x).*besselj(2,x)','x');
>> plot(x,G(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -22 22])
>> r1=fzero(G,[0 1]);r2=fzero(G,[4 6]);r3=fzero(G,[7 10]);
>> disp([r1 r2 r3])
>> % Case 3
>> G=inline('besselj(2,x).*besseli(3,x)+besseli(2,x).*besselj(3,x)','x');
>> plot(x,G(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -22 22])
>> r1=fzero(G,[0 1]);r2=fzero(G,[5 7]);r3=fzero(G,[7 10]);
>> disp([r1 r2 r3])

>> % Case 1
>> G=inline('tan(x)-x+4*x.^3/(2*pi^2)','x');
>> x=linspace(0,15,5000);
>> plot(x,G(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -10 20])
>> r1=fzero(G,[0 1]);r2=fzero(G,[2 4]);r3=fzero(G,[4.75 5]);
>> r4=fzero(G,[7.86 7.87]);r5=fzero(G,[10.998 11]);
>> disp([r1 r2 r3 r4 r5])
>> % Case 2
>> G=inline('tan(x)-x+4*x.^3/(4*pi^2)','x');
>> plot(x,G(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -10 20])
>> r1=fzero(G,[0 1]);r2=fzero(G,[2 4]);r3=fzero(G,[4.75 5]);
>> r4=fzero(G,[7.86 7.9]);r5=fzero(G,[11 11.1]);
>> disp([r1 r2 r3 r4 r5])
>> % Case 3
>> G=inline('tan(x)-x+4*x.^3/(8*pi^2)','x');
>> plot(x,G(x),'k',[0 15],[0 0],'r');
>> axis([0 15 -10 20])
>> r1=fzero(G,[0 1]);r2=fzero(G,[2 4]);r3=fzero(G,[5 5.5]);
>> r4=fzero(G,[7.86 7.96]);r5=fzero(G,[11 11.1]);
>> disp([r1 r2 r3 r4 r5])

>> %case 1
>> G=inline('(1+0.4*x).^2.*(x.^2-x.^3)-0.2','x');
>> x=linspace(0,1,500);
>> plot(x,G(x),'k',[0 1],[0 0],'r');
>> axis([0 1 -1 1])
>> r1=fzero(G,[0.4 0.7]);r2=fzero(G,[0.7 1]);
>> disp([r1 r2]);
>> %using "roots" function
>> n=0.4; m=0.2; %co=n c1=m
>> y=roots([-n^2 (n^2-2*n) (2*n-1) 1 0 -m]);
>> disp(y(y>0))
>> %case 2

>> G=inline('(1+7*x).^2.*(x.^2-x.^3)-4','x');
>> plot(x,G(x),'k',[0 1],[0 0],'r');
>> axis([0 1 -1 1])
>> r1=fzero(G,[0.4 0.7]);r2=fzero(G,[0.7 1]);
>> disp([r1 r2]);
>> %using "roots" function
>> n=7; m=4; %co=n c1=m
>> y=roots([-n^2 (n^2-2*n) (2*n-1) 1 0 -m]);
>> disp(y(y>0))

>> G=inline('2*5^2*sind(b).^2.*tand(b-35).^2-tand(b).*tand(b-35)-

tand(b).^2','b');
>>%This inline statement cannot be broken up as shown. It has been
presented in two lines because of page width restrictions.
>> x=linspace(0,90,5000);
>> plot(x,G(x),'k',[0 90],[0 0],'r');
>> axis([0 90 -0.5 0.5])
>> r1=fzero(G,[0 10]);r2=fzero(G,[10 40]);
>> r3=fzero(G,[40 60]);r4=fzero(G,[60 85]);
>> disp([r1 r2 r3 r4])

>> H=inline('(sum(x.^B.*log(x))./(sum(x.^B)-1/14*sum(log(x)))).^-1-B','x','B');

>> B=linspace(-1,1,length(x));
>> x=[72 82 97 103 113 117 126 127 127 139 154 159 199 207];
>> plot(B,H(x,B),'k',[-1 1],[0 0],'r');
>> syms B
>> y=H(x,B);
>> G=inline(vectorize(y),'B');
>> r=fzero(G,[0.1 0.2]);

>> H=inline('x.*log(sqrt(x.^2-1)+x)-sqrt(x.^2-1)-0.5*x','x');

>> x=linspace(1.01,4,500);
>> plot(x,H(x),'k',[0 4],[0 0],'r');
>> r=fzero(H,[2 3])

Homework problems not solved.

This problem is simple but has much long number as entering data.
For first part, just make an inline function with variable r and tau.
In the second part we have the values of p and tau. Multiply both side of
equation by r and now you have just one variable and you can use fsolve to
find the value of r then put this value and tau in equation of part one you
will find z(r,tau).
For part c do same procedure that done for part two, but this time put tau
as variable.

>> %case 1
>> G=inline('(2*log10(2.51./(1e5*sqrt(y))+0.27/200)).^-2-y','y');
>> y=linspace(0,1,200);
>> plot(y,G(y),'k',[0 1],[0 0],'r')
>> r=fzero(G,[0.01,1]);
>> r=fzero(G,[0.01,1])
>> %case 2
>> G=inline('(2*log10((1e5*sqrt(y))/2.51)).^-2-y','y');
>> plot(y,G(y),'k',[0 1],[0 0],'r')
>> r=fzero(G,[0.01,1])

Homework problems not solved.

>> %As you see this equation has unexpected parenthesis or bracket.
>> %I can't find the correct equation, but solve the problem.
>> %the procedure is correct, but the answer is different. Its a simple problem.

>> G=inline( '(1-(1-cos(x))).*2.*0.6.^1.5.*cos(x)' ,'x');


>> I=1/(2*pi)*quadl(G,-a,a)

>> H=inline('c1./(y.^5.*(exp(c2./(y.*T1))-1))','y','c1','c2','T1');
>> c1=3.742e8;c2=1.439e4;sig=5.667e-8;T1=300;
>> Int=quadl(H,1e-6,150,[],[],c1,c2,T1)
>> sig1=Int/T1^4;
>> %error
>> (sig-sig1)/(sig1) *100;
>>%you can do this procedure for other values of T.

>> H=inline('cos(x-y).*exp(-x.*y./(pi^2))','x','y');
>> I=dblquad(H,0,pi/2,pi/4,pi);

Ode45 differential equation solver (numerically)

function xp=myexp3(t,x)
L=150;m=70;k=10;co=0.00324;g=9.8;
xp=zeros(2,1);

xp(1)=x(2);
xp(2)=-k/m*(x(1)-L).*(x(1)>=L)-co*sign(x(2)).*(x(2).^2)+g;
end
>> [t,x]=ode45('myexp3',[0 15],[0 0]);
>> %the first column of x is distance and the second is velocity;
>> tnew=linspace(0,15,5000);
>> distance=spline(t,x(:,1),tnew);
>> velocity=spline(t,x(:,2),tnew);
>> L=150;m=70;k=10;co=0.00324;g=9.8;
>> acceleration=-k/m*(distance-L).*(distance>L)co*sign(velocity).*(velocity.^2)+g;
>>%The last statement cannot be broken up as shown. It has been
>> %presented in two lines because of page width restrictions.
>>%the sign of answers depends on some other principles.
>> %part a)
>> %using interpolation to find a value that lie between two others,
>> interp1(tnew,distance,11.47);
>> %part b)
>> interp1(tnew,distance,5.988); interp1(tnew,velocity,5.988);
>> %part c)
>> interp1(tnew,acceleration,11.18)

function xp=myexp5(t,x)
M=0;B=10;a=0.1;
xp=zeros(2,1);
xp(1)=x(2);
xp(2)=-a*x(2)+sin(x(1))-B*sin(x(1)).*(1-1./sqrt(5-4*cos(x(1))));
end
>> [t,x]=ode45('myexp5',[0 50],[pi/4 0]);
>> r=linspace(0,50,1000);
>> y=spline(t,x(:,1),r);
>> plot(r,y) %for plot theta as function of t
>> plot(x(:,1),x(:,2)) % for plot theta versus its derivative

function xp=myexp4(t,x)
p=0.375;q=7.4e-4;
xp=zeros(2,1);
xp(1)=x(2);
xp(2)=-sign(x(2)).*p*(x(2).^2)-q*x(1);
end
>> [t,x]=ode45('myexp4',[0 150],[10 0]);
>> interp1(x(:,1),t,0);
>> [t,x]=ode45('myexp4',[0 300],[20 0]);
>> interp1(x((60:80),1),t(60:80),0);
>>%It seems that the answers of problem in book arent precise because
using another method to solve problem. You can check the correct answer
after plot it and zoom in. You can plot the curves with this script.
>> ode45('myexp4',[0 300],[20 0]);

function xp = halfsin(t,x,z)
if t<=5
h=sin(pi*t/5);
elseif t>5
h=0;
end
xp=ones(2,1);
xp(1)=x(2);
xp(2)=-2*z*x(2)-x(1)+h;
end
>> k=0;
>> for z=0.05:0.05:0.95
[t,x]=ode45(@halfsin,[0 35],[1 0],[],z);
k=k+1;
f(k)=sum((x(:,1)-1).^2);
end
>> [x,y]=min(f);
>> z=0.05:0.05:0.95;
>> z(y);

>>%part a)
>> k=0;
>> for B=[0.02,0.05 , 0.08 0.11 0.15 0.18 0.23 0.3]
syms x
H=B./sin(x)+1./cos(x);
H=inline(vectorize(H),'x');
t=fminbnd(H,0,pi/4);
k=k+1;
y(k)=t;
end
>> disp('Corresponding min value of k with respect to B');disp([num2str(y')]);

>> %part b)
>> H=inline('0.16./sin(x)+1./cos(x)-1.5','x');
>> plot(x,H(x),'k',[0 pi/4],[0 0],'r');
>> axis([0 pi/4 -1 1])
>> r1=fzero(H,[0.2 0.5]);r2=fzero(H,[0.5 0.7]);
>> disp([r1 r2]);

>> H=inline('sqrt(1.4/0.4)*sqrt(p.^(2/1.4)-p.^(2.4/1.4))','p');
>> p=linspace(0,1,1000);
>> plot(p,H(p),'k',[0 1],[0 0],'r')
>> [H,index]=max(H(p));
>> maxvalue=p(index)
maxvalue =
0.5285
>> k=1.4;
>> verify=(2/(k+1))^(k/(k-1))
verify =
0.5283

Note:
We use fsolve to solve nonlinear system of equations.
We have to define equations like a matrix in editor window as function.

For fsolve we have to indicate our variables as elements of a vector for


example if we have 3 equations and variables x, y, z, we cant use all
of them in our equations. We select a variable like h and use h(1) , h(2) ,
h(3) as our variables that h refers to the vector after execution of fsolve
function .
%For part (a ) first define function in editor window as below:
function w=myexp(e)
w=[e(1)*(1-cos(e(2)))-3;e(1)*(e(2)-sin(e(2)))-1];
end
>> option=optimset('display' , 'off');
>> z=fsolve(@myexp,[1 1],option);
>> %part b)
>> H=inline('3*(x-sin(x))-(1-cos(x))','x');
>> theta=fzero(H,1);
>> k=3/(1-cos(theta))

function u=myexp2(x)
sig=5.667e-8;T1=373;T2=293;
u=[T1^4-x(1)^4-x(2)/sig; x(1)^4-x(3)^4-x(2)/sig;x(3)^4-T2^4-x(2)/sig];
end
>> option=optimset('display' , 'off');
>> z=fsolve(@myexp2,[10 10 10],option);
>> %part b)
>> A=[1 0 1/sig;1 -1 -1/sig;0 1 -1/sig];
>> B=[T1^4 0 T2^4];
>> x=inv(A)*B';
>> %where x(1)=Ta^4 x(2)=Tb^4
>> Ta=(x(1))^(1/4);Tb=(x(2))^(1/4);Q=x(3);

>> %part a)
>> syms x
>> limit((1-sin(2*x))^(1/x),x,0);

>> %part b)
>> limit(log(x^n)/(1-x^2),x,1)

>> syms a b w
>> x=a+b*cos(w);
>> f=(1-exp(-x))/(1+x^3);
>> g=inline(vectorize(f),'a','b','w');
>> a=1.2;b=-0.45;w=pi/3;
>> g(a,b,w)

>> k=0;
>> for b=linspace(0,4*pi,10)
syms x
y=int((2*x+5)./(x.^2+4*x+5),x,0,b);
k=k+1;
h(k)=y;
end
>> disp(h)

Chapter 6

a. Cycloid
>> phi=linspace(-pi,3*pi,200);ra=[0.5 1 1.5];
>> for n=1:3
x=ra(n)*phi-sin(phi);
y=ra(n)-cos(phi);
subplot(2,2,n);
plot(x,y);
axis equal;
end
b. lemniscate
>> phi=linspace(-pi/4,pi/4,200);
>> x=cos(phi).*sqrt(2*cos(2*phi));
>> y=sin(phi).*sqrt(2*cos(2*phi));
>> plot(x,y)
>> axis equal
c. Spiral
i
>> phi=linspace(0,6*pi,200);
>> x=phi.*cos(phi);
>> y=phi.*sin(phi);
>> plot(x,y)
>> axis equal
ii
>> k=0.1
>> x=exp(k*phi).*cos(phi);
>> y=exp(k*phi).*sin(phi);
>> plot(x,y)
>> axis equal
d. cardioid
>> phi=linspace(0,2*pi,200);
>> x=2*cos(phi)-cos(2*phi);
>> y=2*sin(phi)-sin(2*phi);
>> plot(x,y)
>> axis equal

e. Astroid
>> phi=linspace(0,2*pi,200);
>> x=4*cos(phi).^3;
>> y=4*sin(phi).^3;
>> plot(x,y)
>> axis equal

f. Epicycloid
case 1
>> phi=linspace(0,2*pi,200);
>> Rr=3;ar=[0.5 1 2];
>> for n=1:3
x=(Rr+1)*cos(phi)-ar(n)*cos(phi*(Rr+1));
y=(Rr+1)*sin(phi)-ar(n)*sin(phi*(Rr+1));
subplot(2,2,n);
plot(x,y)
axis equal
end

case 2
>> Rr=2.5;ar=2;phi=linspace(0,4*pi,200);
>> x=(Rr+1)*cos(phi)-ar*cos(phi*(Rr+1));
>> y=(Rr+1)*sin(phi)-ar*sin(phi*(Rr+1));
>> plot(x,y)
>> axis equal

g. Hypocycloid
>> Rr=3;ar=[0.5 1 2];phi=linspace(0,2*pi,200);
>> for n=1:3
x=(Rr-1)*cos(phi)+ar(n)*cos(phi*(Rr-1));
y=(Rr-1)*sin(phi)-ar(n)*sin(phi*(Rr-1));
subplot(2,2,n);
plot(x,y)
axis equal
end

6.2
>> x=linspace(-15,15,50);
>> y=log(abs(sin(x)./x));
>> plot(x,y);

>> x=linspace(-12,7,5000);
>> y=0.001*x.^5+0.01*x.^4+0.2*x.^3+x.^2+4*x-5;
>> n=find(y>0);
>> t=x(n);h=y(n);
>> plot(t,h)

>> dt=linspace(0,5,400);
>> ht=1:0.25:3;
>> [hht,ddt]=meshgrid(ht,dt);
>> c=0.5*ddt.^3-1.5*hht.*ddt.^2+(1+hht.^2).*ddt;
>> plot(dt,c)
>> axis([0 5 0 8])

Homework problems not solved.

% we can solve this problem without using for loop, but it maybe have too
long script.
function nonoverlapping(d,w,L)
b=atan((w/2)/(d+L));
a=atan(w/(2*d));
r1=sqrt(d^2+(w/2)^2);
r2=sqrt((d+L)^2+(w/2)^2);

t=2*pi/floor(pi/a);
for n=0:(floor(pi/a)-1)
p1=cos(n*t)*d ;p2= r1*cos(n*t+a);p3= sin(n*t)*d ;p4= r1*sin(n*t+a);
x=[0 p1 p2 p2+L*cos(n*t) r2*cos(-b+(n*t)) r2*cos(-b+n*t)-L*cos(n*t) p1];
y=[0 p3 p4 p4+L*sin(n*t) r2*sin(-b+(n*t)) r2*sin(-b+n*t)-L*sin(n*t) p3];
plot(x,y,'k')
hold on
end
axis equal
end

%case 1
>>v=0.3; % z/a=u
>>F=inline('-((1-u.*atan(1./u))*(1-v)-0.5*(1+(u).^2).^-1)','u','v');
>>plot(u,F(u,v));
>>plot(u,F(u,v));
>>hold on
>>xlabel('u');
>>ylabel('\it\sigma_{\rmx}\rm/ \itp_{\rmmax}');
>>%case 2
>>G=inline('-(1+u.^2)','u');
>>plot(u,G(u));
>>hold on
>>xlabel('u');
>>ylabel('\it\sigma_{\rmz}\rm/ \itp_{\rmmax}');
>>%case 3
>>H=inline('0.5*(-((1-u.*atan(1./u))*(1-v)-0.5*(1+(u).^2).^-1)- (1+u.^2))','u','v');
>>plot(u,H(u,v));
>>hold on
>>xlabel('u');
>>ylabel('\it\tau_{\rmxy}\rm/ \itp_{\rmmax}\rm= \it\tau_{\rmyz}\rm/
\itp_{\rmmax}');
>>%These last statements cannot be broken up as shown. They have been
>> %presented in two lines because of page width restrictions.

%case 1
>>v=0.3; % z/b=u
>>u=linspace(0,10,1000);
>>F=inline('-2*v*(sqrt(1+u.^2)-u)','u','v');
>>plot(u,F(u,v));
>>hold on
>>xlabel('u');
>>ylabel('\it\sigma_{\rmx}\rm/ \itp_{\rmmax}');
>>%case 2
>>G=inline('-((2-1./(1+u.^2)).*sqrt(1+u.^2)-2*u)','u');
>>plot(u,G(u));
>>xlabel('u');
>>ylabel('\it\sigma_{\rmy}\rm/ \itp_{\rmmax}');
>>%case 3
>>H=inline('-1./sqrt(1+u.^2)','u');
>>plot(u,H(u));
>>xlabel('u');
>>ylabel('\it\sigma_{\rmz}\rm/ \itp_{\rmmax}');
>>%case 4
>>K=inline('0.5*(-((2-1./(1+u.^2)).*sqrt(1+u.^2)-2*u)- -1./sqrt(1+u.^2))','u')

>>plot(u,K(u));
>>xlabel('u');
>>ylabel('\it\tau_{\rmyz}\rm/ \itp_{\rmmax}');

>> A=[9.1209 9.1067 8.9939 8.9133 8.5194 8.3666];


>> B=[3.5605 3.5385 3.4777 3.4292 3.2621 3.1884];
>> SAE=10:10:60;
>> h=linspace(10,60,1000);
>> Aa=spline(SAE,A,h);
>>Ba=spline(SAE,B,h);
>>T=linspace(0,500,1000);
>>To=255.2+5/9*T;
>> c=10.^(Aa-Ba.*log10(To));
>> u=10.^(c-1);

>> %case 1
>> subplot(1,2,1);
>> plot(To,u)
>> %case 2
>> subplot(1,2,2);
>> plot(log10(To),log10(log10(10*u)))

>>B=[0.02 0.05 0.08 0.11 0.15 0.18 0.23 0.3];


>>y=linspace(1,40,500);
>> [yy,BB]=meshgrid(y,B);
>>K=BB./sind(yy)+1./cosd(yy);
>>%recall from exercise 5.32 for finding min vaules
>>k=0;
>>for B=[0.02,0.05 , 0.08 0.11 0.15 0.18 0.23 0.3]
syms x
H=B./sin(x)+1./cos(x);
H=inline(vectorize(H),'x');
t=fminbnd(H,0,pi/4);
k=k+1;
h(k)=t;
end
>>h=h*180/pi;
>>u=linspace(h(1),h(end),500);
>>for n=1:length(h)
w(n)=interp1(y,K(n,:),h(n));
end
>>p=spline(h,w,u);
>>plot(u,p,'k--')
>>hold on
>>plot(y,K,'k')
>>axis([0 40 1 2])
>>xlabel('\rm\lambda');
>>ylabel('\rm\kappa');
>>legend('Minimum','location','southwest')

>> rs=1.5*sin(pi/5)/(1-sin(pi/5));
>> t=linspace(0,2*pi,3000);
>> plot(1.5*cos(t),1.5*sin(t),'k',0,0,'k+')
>> axis equal
>> hold on
>> for n=0:4
a=cosd(72*n)*(1.5+rs);
b=sind(72*n)*(1.5+rs);
x=a+rs*cos(t);y=b+rs*sin(t);
plot(x,y,'k',a,b,'k+')
end
>>%we can make a function to do this procedure for desire value of rb
and n as below.
function ciraroundcir(rb, n)
rs=rb*sin(pi/n)/(1-sin(pi/n));
t=linspace(0,2*pi,3000);
plot(rb*cos(t),rb*sin(t),'k',0,0,'k+')
axis equal
hold on
for m=0:(n-1)
a=cos(2*pi/n*m)*(rb+rs);
b=sin(2*pi/n*m)*(rb+rs);
x=a+rs*cos(t);y=b+rs*sin(t);
plot(x,y,'k',a,b,'k+')
end
end

>> t=linspace(0,2*pi,100);
>> plot([3+cos(t)],[1+sin(t)])
>> axis equal
>> hold on
>> fill([3+cos(t)],[1+sin(t)],'g')

>> plot([0 5],[-5 5])


>> h=linspace(0,5,100);
>> i=fliplr(h);
>> fill([h,i],[2*h-5 , (zeros(1,length(i))-5)],'r')
>> axis([-2 5 -5 4])
>> r=0.5:0.5:3;
>> x=1+cos(t)'*r;
>> y=1+sin(t)'*r;
>> plot(x,y,'b',1,1,'b+')
>> plot([-2 5],[0 0],'k',[0 0],[-5 4],'k')
>> xlabel('\itx_{\rm1}')
>> ylabel('\itx_{\rm2}')
>> title('Blue circles:function minimized. Green area: feasible region')

>> x=linspace(0,pi,200);
>> plot(x,[sin(x) ;abs(sin(2*x))/2],'k')
>> hold on
>> fill([x,fliplr(x)],[sin(x),abs(sin(2*x))/2],'c')
>> axis([0 pi 0 1])
>> plot([0 pi],[0.5 0.5],'k--')

You might also like