Problem 1 a :(sum of first 5 numbers):
sum=0;
for n=1:5
sum=sum+n
end
output:
sum =
15
Problem 1 b (odd numbers):
m=1;
fprintf('odd numbers from 3 to 25 are:');
while (m<25)
fprintf('%d\n');
m=m+2
end
output:
odd numbers from 1 to 25 are:
m =
m =
m =
m =
m =
11
m =
13
m =
15
m =
17
m =
19
m =
21
m =
23
m =
25
Problem 2:
a=1;
b=2;
sum=a+b;
if sum>10
fprintf('sum is greater than 10')
elseif sum<10
fprintf('sum is less than 10')
else
fprintf('sum is = 10')
end
output:
sum is less than 10
Problem 3:
m=95;
switch (m)
case 95
fprintf("Excellent")
case 85
fprintf("Good")
case 75
fprintf("well done")
case 65
fprintf("passed")
otherwise
fprintf("better try again")
end
output:
Excellent
Problem 4:
x = [0:0.01:20];
y = cos(x);
plot(x, y), xlabel('x'), ylabel('Cos(x)'), title('Cos(x) Graph')
Output:
Problem 5:
theta = 0:0.01:2*pi;
rho = 1 + cos(theta);
polar(theta, rho)
Output:
Problem 6:
syms t
xt = (t);
yt = cos(t);
zt = sin(t);
fplot3(xt,yt,zt)
output:
Problem 7:
[x,y] = meshgrid(-4:.4:4);
z=sin(y)+cos(x);
surf(x,y,z)
output:
Problem 8:
a.
syms y(t)
ode = (diff(y,t)+y)^2 == 1;
cond = y(0) == 0;
ySol(t) = dsolve(ode,cond)
output:
ySol(t) =
exp(-t) - 1
1 - exp(-t)
b.
syms y(x)
Dy = diff(y);
ode = diff(y,x,2) == cos(2*x)-y;
cond1 = y(0) == 1;
cond2 = Dy(0) == 0;
conds = [cond1 cond2];
ySol(x) = dsolve(ode,conds);
ySol = simplify(ySol)
output:
ySol(x) =
1 - (8*sin(x/2)^4)/3
c.
ode = diff(u,x,3) == u;
cond1 = u(0) == 1;
cond2 = Du(0) == -1;
cond3 = D2u(0) == pi;
conds = [cond1 cond2 cond3];
uSol(x) = dsolve(ode,conds)
output:
uSol(x) =
(pi*exp(x))/3 - exp(-x/2)*cos((3^(1/2)*x)/2)*(pi/3 - 1) - (3^(1/2)*exp(-
x/2)*sin((3^(1/2)*x)/2)*(pi + 1))/3
d.
syms y(x)
ode = diff(y,x,2) == x*y;
ySol(x) = dsolve(ode)
output:
ySol(x) =
[ empty sym ]
Problem 9:
z= @(x,y) exp(-(x+2*y).^2);
z(-1,1)
output:
ans =
0.3679
Problem 10:
syms x y a b p q
z=(x^2/a^2)+(y^2/b^2)
eq1=p==diff(z,x)
c1=solve(eq1,a)
eq2=p==diff(z,y)
c2=solve(eq2,b)
pde=subs(z,a,c1)
pde=subs(pde,b,c2)
output:
z =
x^2/a^2 + y^2/b^2
eq1 =
p == (2*x)/a^2
c1 =
2^(1/2)*(x/p)^(1/2)
-2^(1/2)*(x/p)^(1/2)
eq2 =
p == (2*y)/b^2
c2 =
2^(1/2)*(y/p)^(1/2)
-2^(1/2)*(y/p)^(1/2)
pde =
(p*x)/2 + y^2/b^2
(p*x)/2 + y^2/b^2
pde =
(p*x)/2 + (p*y)/2
(p*x)/2 + (p*y)/2
(p*x)/2 + (p*y)/2
(p*x)/2 + (p*y)/2
Problem 11:
syms x y
int(int(sin(x)*sin(y),x),y)
output:
ans =
cos(x)*cos(y)
Problem 12:
m = 0;
x = linspace(0,2,40);
t = linspace(0,1,50);
u = pdepe(m,@eqn1,@initial1,@bc1,x,t);
surf(x,t,u);
title('Surface plot of solution.');
xlabel('Distance x');
ylabel('Time t');
function [c,b,s] = eqn1(x,t,u,DuDx)
c = 1;
b = DuDx;
s = 0;
end
function [pl,ql,pr,qr] = bc1(xl,ul,xr,ur,t)
pl = ul;
ql = 0;
pr = ur-1;
qr = 0;
end
function value = initial1(x)
value = 2*x/(1+x^2);
end
output:
Problem 13:
x=[2:0.01:2.1];
y=gamma(x)
output:
y =
1.0000 1.0043 1.0086 1.0131 1.0176 1.0222 1.0269 1.0316
1.0365 1.0415 1.0465
Problem 14:
fplot(@gamma)
hold on
fplot(@(x) 1.-gamma(x))
ylim([-20 30])
legend('\Gamma(x)','1-\Gamma(x)')
hold off
grid on
output:
Problem 15:
Z = [0.7 0.02 0.05 1];
W = 0:0.05:10;
B = zeros(4,201);
for i = 1:4
B(i,:) = beta(Z(i),W);
end
plot(W,B)
title('Beta function for z = 0.7 0.02 0.05 and 1 is')
output: