Showing posts with label MAE11. Show all posts
Showing posts with label MAE11. Show all posts

Monday, November 13, 2023

Line Integrals

 Line integral

§  Because the line integral needs to be computed repeatedly, it is advisable to encapsulate it within a function.

To create a function

·         The function should begin as follows:

function [y1, ..., yN] = fun_name(x1, ..., xM)

·         This declares a function named fun_name that accepts inputs x1, ..., xM and returns outputs y1, ..., yN. The function should end with end.

·         Function definition should be at the end of the main program.

·         To use the function, call it from the main main program as many times as needed.

%%%%%%%%%%%%%%%% Main Program %%%%%%%%%%%%%%%%%%

clc

clear

syms x y

LInt=0;

%Get the number of sub path in the curve C

n=input("How many subcurves are ther in the curve C? :")

%Calculate the line integral along each path

for i=1:n

    line(i)=LineIntegral(x,y);

    LInt =vpa(LInt +line(i))

end

%%%%%%%%%%%%%%%% Main Program ends %%%%%%%%%%%%%%%%%%

%Define function for Line integral

function line = LineIntegral(x,y)

option=input("1: y=f(x)\n2: x=g(y)\n")

 

switch option

    case 1

        y=input("Enter the function f(x): \n")

        r=[x y];

        F=input("Enter the vector function F(x,y)=[F1 F2]: F(x,y)")

        dr=diff(r,x);

        integrand1=dot(F,dr);

        xL=input("Enter the lower limit of x:");

        xU=input("Enter the Upper limit of x:");

        line=int(integrand1,x,xL,xU)

    case 2

        x=input("Enter the function g(y): \n")

        r=[x y];

        F=input("Enter the vector function F(x,y)=[F1 F2]: F(x,y)")

        dr=diff(r,y);

        integrand1=dot(F,dr);

        yL=input("Enter the lower limit of y:");

        yU=input("Enter the Upper limit of y:");

        line=int(integrand1,y,yL,yU)

    otherwise

        fprintf("Give the proper input")

end

 

end

%%%%%%%%%%%%%%%% Line Integral definition ends %%%%%%%%%%%%%%%%%%

Example 1

$$\text{Evaluate }\int F\cdot dr$$ $$\text{where c is the curve in plane }y=2x^2\text{ from }(0,0)\text{  to }(1,2)\text{  and }F=3xy\hat{i}-y^2 \hat{j}$$

How many subcurves are there in the curve C? :1

n =      1

1: y=f(x)

2: x=g(y)

1

option =      1

Enter the function f(x):

2*x^2

y =

2*x^2

Enter the vector function F(x,y)=[F1 F2]: F(x,y)=[3*x*y -y^2]

F =

[6*x^3, -4*x^4]

Enter the lower limit of x:0

Enter the Upper limit of x:1

line =

-7/6

LInt =

-1.1666666666666666666666666666667

Line integral using the parametric form

clear

clc

syms x y z t

x=input("Enter x(t):"); % If x ,y, z is a function of ‘t’

y=input("Enter y(t):");

z=input("Enter z(t):");

 

r=[x y z];

F=input("Enter the vector function F(x,y,z):")

dr=diff(r,t);

integrand1=dot(F,dr);

tl=input("Enter the lower limit of t:");

tu=input("Enter the Upper limit of t:");

line=int(integrand1,t,tl,tu)

Example 1

$$\text{Evaluate }\int F\cdot dr$$ $$\text{where c is the curve given by }x=2t^2, y=t, z=t^3$$

$$\text{from (0,0,0) to (2,1,1) and } F=(2y+3)\hat{i}+xz\hat{j}+(yz-x)\hat{k}.$$

Output

Enter x(t):2*t^2

Enter y(t):t

Enter z(t):t^3

Enter the vector function F(x,y,z):[2*y+3 x*z yz-x]

Enter the vector function F(x,y,z):[2*y+3 x*z y*z-x]

F =

[3 + 2*t, 2*t^5, - 2*t^2 + t^4]

Enter the Lower limit of t:0

Enter the Upper limit of t:1

line =

288/35





Sunday, November 12, 2023

Linear Algebra :Gauss-Seidel and Rayleigh power method

 

Solving the system of linear equations using Gauss-Seidel method

clc;

close;

clear;

 

a = input("Enter the co-efficient matrix:");

b = input("Enter the constant matrix:");

n = size(a);

oldx = input("Initial guesses:");

x = oldx;

 

for k = 1:20

    fprintf("\n Iteration %d:\t ", k)

    for i = 1:n

        sum = 0;

        for j = 1:n

            if i~=j

                sum = sum+a(i,j) * x(j);

            end

        end

        x(i) = (b(i)-sum)/a(i,i);

        fprintf("x(%d)=%0.4f\t",i,x(i))

    end

    if abs(max(x)-max(oldx))<=0.0001

        break;

    else

 

oldx = x;

    end

end

Note: Enter the coefficient matrix after making matrix diagonally dominant

Example

Use Gauss Siedel method to solve: $$5x+2y+z=1; x+4y+2z=0; x+2y+5z=3$$ considering the initial guess (1,0,3)

Output

Enter the co-efficient matrix:[5 2 1;1 4 2;1 2 5]

Enter the constant matrix:[1;0;3]

Initial guesses:[1;0;3]

 Iteration 1: x(1)=-0.4000 x(2)=-1.4000 x(3)=1.2400 

 Iteration 2: x(1)=0.5120 x(2)=-0.7480 x(3)=0.7968 

 Iteration 3: x(1)=0.3398 x(2)=-0.4834 x(3)=0.7254 

 Iteration 4: x(1)=0.2483 x(2)=-0.4248 x(3)=0.7202 

 Iteration 5: x(1)=0.2259 x(2)=-0.4166 x(3)=0.7215 

 Iteration 6: x(1)=0.2223 x(2)=-0.4163 x(3)=0.7221 

 Iteration 7: x(1)=0.2221 x(2)=-0.4166 x(3)=0.7222 

 Iteration 8: x(1)=0.2222 x(2)=-0.4166 x(3)=0.7222


Rayleigh power method

Program to find the numerically largest eigenvalue and eigenvector using Rayleigh power method

clc;

clear

n = input("Enter the number of varibles:");

a = input("Enter the  matrix:");

x0 = input("Initial guesses:");

 

for k = 1:5

    C=a*x0;

        m=max(abs(C));

        x=(C./m);

if abs((x)-(x0))<=0.0001

break;

else

x0 = x;

end

fprintf("---------------------------------------------\nIteration %d :\n",k);

fprintf("The Eigen vector:\n");

disp(x);

fprintf("The largest Eigen value: %f\n",m);

end

Example

Find the numerically largest eigenvalue and eigenvector of


by taking [1 0 0]' as the initial approximation using Rayleigh power method

Output

Enter the number of varibles:3

Enter the  matrix:[2 0 1;0 2 0; 1 0 2]

Initial guesses:[1; 0 ;0]

---------------------------------------------

Iteration 1 :

The Eigen vector:

    1.0000

         0

    0.5000

The largest Eigen value: 2.000000

---------------------------------------------

Iteration 2 :

The Eigen vector:

    1.0000

         0

    0.8000

The largest Eigen value: 2.500000

---------------------------------------------

Iteration 3 :

The Eigen vector:

    1.0000

         0

    0.9286

The largest Eigen value: 2.800000

---------------------------------------------

Iteration 4 :

The Eigen vector:

    1.0000

         0

    0.9756

The largest Eigen value: 2.928571

---------------------------------------------

Iteration 5 :

The Eigen vector:

    1.0000

         0

    0.9918

The largest Eigen value: 2.975610

 








Friday, October 20, 2023

i) Angle between radius vector and tangent vector ii) Angle of intersection between two polar curves

i) Angle between radius vector and tangent vector

Algorithm:

·       For a given curve $$r=f(\theta)$$, find $$\phi=cot^-1(\frac{r'}{r})$$, where $$r'=dr/d\theta$$

clear

clc

close

syms theta

%Get the curve

r1=input("Enter the first curve r1(theta): r1=");

%Find phi1

phi1=simplify(acot(diff(r1)/r1));

%display phi1

fprintf("\n Angle between radius vector and tangent vector to the curve %s:\n phi= %s",r1,phi1)

 

Output

Enter the first curve r1(theta): r1=2*(1+cos(theta))

Angle between radius vector and tangent vector to the curve 2*cos(theta) + 2:

 phi1= -acot(sin(theta)/(cos(theta) + 1))

 

ii) Angle of intersection between two polar curves

Algorithm:

·      For a given curve $$r_1=f_1(\theta)$$ and $$r_2=f_2(\theta)$$ find, $$\phi=cot^{-1}(\frac{r'}{r})$$

 where $$r'=dr/d\theta$$

·       Solve given curves for theta value

·       Get the required appropriate theta value from the solution

·       Find $$|\phi_2-\phi_1|$$ by substituting obtained  theta value

clear

clc

close

syms theta

 

%Get the two curves

r1=input("Enter the first curve r1(theta): r1=");

r2=input("Enter the second curve r2(theta): r2=");

%Find phi1 and phi2

 

phi1=simplify(acot(diff(r1)/r1));

phi2=simplify(acot(diff(r2)/r2));

 

%display phi1 and phi2

fprintf("\n Angle between radius vector and tangent vector to the curve %s:\n phi1= %s",r1,phi1)

fprintf("\n Angle between radius vector and tangent vector to the curve %s:\n phi2= %s",r2,phi2)

 

%Find t(point of intersection)

fprintf('\n The values of theta at the point of intersection are: ')

S=solve(r1==r2,theta,'Real',true)

 

%Get the theta

tt=input("\n Choose the value of theta: ");

 

%Calculate the angle between the given curves

ang1= abs(vpa(subs(phi1-phi2, {theta},{tt})));

ang2=vpa(pi-ang1);

fprintf('\n Angle between given polar curves = %f or %f \n', ang1, ang2);

Output

Enter the first curve r1(theta): r1=2*(1+cos(theta))

Enter the second curve r2(theta): r2=2*(1-cos(theta))

Angle between radius vector and tangent vector to the curve 2*cos(theta) + 2:

phi1= -acot(sin(theta)/(cos(theta) + 1))

Angle between radius vector and tangent vector to the curve 2 - 2*cos(theta):

phi2= -acot(sin(theta)/(cos(theta) - 1))

The values of theta at the point of intersection are: S =

pi/2

Choose the value of theta: pi/2

Angle between given polar curves = 1.570796 or 1.570796

 


Search This Blog