0% found this document useful (0 votes)
4 views

Matlab2

The document contains a series of MATLAB assignments focusing on mathematical concepts such as partial differentiation, matrix operations, consistency of systems of equations, and vector calculus including gradient, divergence, and curl. Each assignment includes code snippets, outputs, and evaluations of mathematical expressions. The assignments cover a range of topics including directional derivatives and solving ordinary differential equations.

Uploaded by

anitajana127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Matlab2

The document contains a series of MATLAB assignments focusing on mathematical concepts such as partial differentiation, matrix operations, consistency of systems of equations, and vector calculus including gradient, divergence, and curl. Each assignment includes code snippets, outputs, and evaluations of mathematical expressions. The assignments cover a range of topics including directional derivatives and solving ordinary differential equations.

Uploaded by

anitajana127
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Matlab

Assignment 1
Q partial differentiation of the eq. f= e^(x2+y2)
syms x y
f=exp(x.^2+y.^2)
P=diff(f,x)
R=diff(f,y)
P1=diff(P,x)
R1=diff(R,y)
Q1=subs(P1, {x,y},{0.5.*pi,0.5.*pi})
Q2=subs(R1, {x,y},{0.5.*pi,0.5.*pi})
if (Q1==Q2)
display('Mixed Partial Derivatives are Equal')
end
if (Q1 ~= Q2)
display('Mixed partial Derivatives are not equal')
end

output

>> partialderivative Q1 =
f= 2*exp(pi^2/2) + pi^2*exp(pi^2/2)
exp(x^2 + y^2) Q2 =
P= 2*exp(pi^2/2) + pi^2*exp(pi^2/2)
2*x*exp(x^2 + y^2) Mixed Partial Derivatives are Equal
R= >>
2*y*exp(x^2 + y^2)
P1 =
2*exp(x^2 + y^2) + 4*x^2*exp(x^2 + y^2)
R1 =
2*exp(x^2 + y^2) + 4*y^2*exp(x^2 + y^2)
Assignment 2
A = [1 2 3 ; 4 5 6 ; 7 8 9 ]

m = size (A)

n = min (m)

R=rank(A)

D = det(A)

if (D~=0)

B = inv(A)

end

OUTPUT

>> AASSIN6
A= B=
1 2 3 1.0e+16 *
4 5 6
7 8 9 -0.4504 0.9007 -0.4504
m= 0.9007 -1.8014 0.9007
-0.4504 0.9007 -0.4504
3 3
n= >>

3
R=

2
D=
6.6613e-16
ASSIGNMENT 3
q. x+y = 4
y-z = 1 Comment upon the consistency/inconsistency by evaluating the rank.
2x+y+4z = 7
A = [1 1 0; 0 1 -1; 2 1 4]
B = [4; 1; 7]
A_bar = [1 1 0 4; 0 1 -1 1; 2 1 4 7]
rank_A = rank(A);
rank_A_bar = rank(A_bar);

if rank_A == rank_A_bar
disp('The system is consistent.');
if rank_A == size(A,2)
disp('The system has a unique solution.');
X = A\B;
disp('Solution:');
disp(X);
else
disp('The system has infinitely many solutions.');
end
else
disp('The system is inconsistent.');
end
output
A= A_bar =
1 1 0 1 1 0 4
0 1 -1 0 1 -1 1
2 1 4 2 1 4 7
B= The system is consistent.
4 The system has a unique solution.
1 Solution:
7 3
1
0
ASSIGNMENT 3
. x+2y-z = 10
-x+y+2z = 2 Comment upon the consistency/inconsistency by evaluating the rank.
2x+y-3z = 8
A = [1 2 -1; -1 1 2; 2 1 -3]
B = [10;2;8]
A_bar = [1 2 -1 10; -1 1 2 2; 2 1 -3 8]
rank_A = rank(A);
rank_A_bar = rank(A_bar);

if rank_A == rank_A_bar
disp('The system is consistent.');
if rank_A == size(A,2)
disp('The system has a unique solution.');
X = A\B;
disp('Solution:');
disp(X);
else
disp('The system has infinitely many solutions.');
end
else
disp('The system is inconsistent.');
end
output
A= A_bar =
1 2 -1 1 2 -1 10
-1 1 2 -1 1 2 2
2 1 -3 2 1 -3 8
B=
10 The system is consistent.
2 The system has infinitely many solutions.
8
ASSIGNMENT 4
x+y+z = 1
2x+y+2z = 2 Comment upon the consistency/inconsistency by evaluating the rank.
3x+2y+3z = 5
code
A = [1 1 1; 2 1 2; 3 2 3]
B = [1;2;5]
A_bar = [1 1 1 1; 2 1 2 2; 3 2 3 5]
rank_A = rank(A);
rank_A_bar = rank(A_bar);
if rank_A == rank_A_bar
disp('The system is consistent.');
if rank_A == size(A,2)
disp('The system has a unique solution.');
X = A\B;
disp('Solution:');
disp(X);
else
disp('The system has infinitely many solutions.');
end
else
disp('The system is inconsistent.');
end
output

A= A_bar =
1 1 1
2 1 2 1 1 1 1
3 2 3 2 1 2 2
B= 3 2 3 5

1 The system is inconsistent.


2
5
ASSIGNMENT
q. f(x,y,z)=x2+y2+z2;
F=xyî+yzĵ+xzz Find the gradient, divergence & curl.
syms x y z
f = x^2+y^2+z^2
F=[x*y, y*z, z*x]
grad_f=gradient(f,[x y z])
div_F=divergence(F,[x y z])
curl_F=curl(F,[x y z])
grad_f
div_F
curl_F
output

f=
x^2 + y^2 + z^2
F=
[x*y, y*z, x*z]
grad_f =
2*x
2*y
2*z
div_F =
x+y+z
curl_F =
-y
-z
-x
grad_f =
2*x
2*y
2*z
div_F =
x+y+z
curl_F =
-y
-z
-x

ASSIGNMENT
Q F= x2yî-2xzĵ+2yzz Find the curl curl F.

syms x y z

F=[x^2*y, -2*x*z, 2*y*z]

curl_F= curl(F,[x y z])

curl_curlF= curl(curl_F,[x y z])

output

F=

[x^2*y, -2*x*z, 2*y*z]

curl_F =

2*x + 2*z
0
- x^2 - 2*z

curl_curlF =

0
2*x + 2
0
ASSIGNMENT
Q F= grad(x3+y3+z3-3xyz) Find the div F, curl F.

syms x y z

f = x^3+y^3+z^3-3*x*y*z

F=gradient(f,[x y z])

div_F=divergence(F,[x y z])

curl_F=curl(F,[x y z])

output

f=
x^3 - 3*x*y*z + y^3 + z^3
F=
3*x^2 - 3*y*z
3*y^2 - 3*x*z
3*z^2 - 3*x*y
div_F =

6*x + 6*y + 6*z

curl_F =

0
0
0
ASSIGNMENT
f=x2y+2xy+z2 Show that curl grad f=0.

syms x y z

f = x*x*y+2*x*y+z*z;

grad_f = gradient(f, [x y z])

curl_f=curl(grad_f,[x y z])

output

grad_f =

2*y + 2*x*y
x^2 + 2*x
2*z

curl_f =

0
0
0
ASSIGNMENT
Q Find the Directional derivative of x*y + y*z + z*x at (1, 2, 0) in the direction of 1, 2, 2.

syms x y z;
theta = x*y + y*z + z*x;
point = [1, 2, 0];
direction = [1, 2, 2];
grad_theta = gradient(theta, [x, y, z]);
grad_theta_at_point = subs(grad_theta, [x, y, z], point);
direction_unit = direction / norm(direction);
D_theta = dot(grad_theta_at_point, direction_unit);
fprintf('Directional derivative for Problem 1: %f\n', double(D_theta));
output
Directional derivative for Problem 1: 3.333333
ASSIGNMENT
Q Find the Directional derivative 2*x^2 + 3*y^2 + z^2 at (2, 1, 3) in the direction of 1, 0, -2.

syms x y z;
f = 2*x^2 + 3*y^2 + z^2;
point = [2, 1, 3];
direction = [1, 0, -2];
grad_f = gradient(f, [x, y, z]);
grad_f_at_point = subs(grad_f, [x, y, z], point);
direction_unit = direction / norm(direction);
D_f = dot(grad_f_at_point, direction_unit);
fprintf('Directional derivative for Problem 3: %f\n', double(D_f));
output
Directional derivative for Problem 3: -1.788854
ASSIGNMENT
Q Find the Directional derivative 2*x^2 + 3*y^2 + z^2 at (2, 1, 3) in the direction of 1, 0, -2.

dydx = @(x,y) x + y*(cos(x))^2


y0 = 1
xspan = [0 5]
[x ,y] = ode45(dydx ,xspan, y0)
y
figure;
plot(x,y ,'b')
xlabel('x')
ylabel('y')
title('solution')
output
dydx = 1.8260 y= 11.2173
function_handle with 1.9510 13.0842
value: 2.0760 1.0000 15.2409
@(x,y)x+y*(cos(x))^2 2.2010 1.0528 17.6701
y0 = 2.3260 1.1105 20.3197
1 2.4510 1.1733 23.1081
xspan = 2.5760 1.2409 25.9268
0 5 2.7010 1.4288 28.6483
2.8260 1.6411 31.1435
x= 2.9510 1.8713 33.3114
3.0760 2.1112 35.0914
0 3.2010 2.3520 36.4686
0.0502 3.3260 2.5856 37.4908
0.1005 3.4510 2.8064 38.2464
0.1507 3.5760 3.0128 38.8576
0.2010 3.7010 3.2065 39.2151
0.3260 3.8260 3.3941 39.6061
0.4510 3.9510 3.5853 40.0637
0.5760 4.0760 3.7928 40.6210
0.7010 4.2010 4.0319
0.8260 4.3260 4.3195
0.9510 4.4510 4.6750
1.0760 4.5760 5.1198
1.2010 4.7010 5.6783
1.3260 4.7757 6.3780
1.4510 4.8505 7.2493
1.5760 4.9252 8.3247
1.7010 5.0000 9.6372
ASSIGNMENT
dydx = @(x,y) x/y^3 - y/2*x
y1 = 2
xspan = [ 0 10 ]
[x ,y] = ode45(dydx ,xspan, y1)
y
figure;
plot( x , y ,'r')
xlabel('x')
ylabel('y')
title('solution')
output
dydx = 1.7135 y= 1.1893
1.7826 1.1894
function_handle with 1.8517 2.0000 1.1889
value: 1.9208 1.9727 1.1887
1.9899 1.8950 1.1894
@(x,y)x/y^3-y/2*x 2.0590 1.7773 1.1897
2.1281 1.6354 1.1893
2.1972 1.5410 1.1891
y1 = 2.2955 1.4516 1.1893
2.3939 1.3729 1.1895
2 2.4922 1.3090 1.1892
2.5905 1.2869 1.1890
2.6828 1.2677 1.1893
xspan = 2.7751 1.2516 1.1894
2.8674 1.2381 1.1887
0 10 2.9596 1.2270 1.1885
3.0698 1.2181 1.1894
3.1799 1.2110 1.1899
x= 3.2900 1.2055 1.1894
3.4001 1.1994 1.1891
0 3.5301 1.1953 1.1893
0.2500 3.6601 1.1933 1.1895
0.5000 3.7900 1.1921 1.1892
0.7500 3.9200 1.1908 1.1891
1.0000 4.0096 1.1901 1.1893
1.1611 4.0992 1.1898 1.1894
1.3222 4.1888 1.1897 1.1890
1.4833 4.2784 1.1893 1.1888
1.6444 4.3681 1.1891 1.1893
ASSIGNMENT
dydx=@(x,y)(1+(2.71).^(2*x)).^(-1)-y
y0=1
xspan=[0,10]
[x,y]=ode45(dydx,xspan,y0)
figure;
plot(x,y,'b')
xlabel('x')
ylabel('y')
title('solution')

dydx = 2.1519 y= 0.0033


2.4019 0.0027
function_handle with 2.6253 1.0000 0.0022
value: 2.8488 0.9498 0.0018
3.0722 0.8996 0.0015
3.2956 0.8497 0.0012
@(x,y)(1+(2.71).^(2*x)).^(- 3.4993 0.8001 0.0010
1)-y 3.7029 0.6801 0.0008
3.9065 0.5683 0.0007
4.1102 0.4679 0.0006
y0 = 4.3096 0.3805 0.0005
4.5091 0.3063 0.0004
1 4.7085 0.2446 0.0003
4.9080 0.1944 0.0003
5.1058 0.1539 0.0002
xspan = 5.3037 0.1244
5.5015 0.1003
0 10 5.6993 0.0808
5.8965 0.0650
6.0936 0.0532
x= 6.2908 0.0435
6.4879 0.0356
0 6.6848 0.0291
0.1005 6.8816 0.0239
0.2010 7.0785 0.0196
0.3014 7.2753 0.0161
0.4019 7.4720 0.0132
0.6519 7.6687 0.0108
0.9019 7.8654 0.0089
1.1519 8.0621 0.0073
1.4019 8.2588 0.0060
1.6519 8.4554 0.0049
1.9019 8.6521 0.0040
OUTPUT

You might also like