0% found this document useful (0 votes)
7 views9 pages

Comp Mechanics Antony

Uploaded by

antonyjasfer649
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)
7 views9 pages

Comp Mechanics Antony

Uploaded by

antonyjasfer649
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/ 9

Computational

Mechanics-1
In the equation P*V = n*R*T, the variables are given as P=10, n=2, R=7,
and T= ½. Write a MATLAB code to find V according to the given formula.

Solution: The following code can be used to find the V value.

>>
P=10;
n=2;
R=7;
T=1/2;
V=(n*R*T)/P

Output:
V=

0.7000
Computational
Mechanics-1

Write a MATLAB code to plot the function y=2sinx.

Solution: The following code can be used to plot

>> x=linspace(0,pi,100);
y=2.*sin(x);
plot(x,y)
Computational
Mechanics-1
Output:
Computational
Mechanics-1

Write a MATLAB code to plot the function y = sinx with grids. Then, keep
the
first graph and plot a second function given by y = cos (x) within the same interval.
Insert the labels for each data set, as well.

Solution. The desired plot can be obtained using the following program and the results
are given below.

>>x=linspace(0,pi,100
); y=2.*sin(x); plot(x,y)
hold y2=cos(x);
plot(x,y2) title('Title
comes here')
xlabel('This is x label')
ylabel('This is y label')
legend('2sin(x)','cos(x)'
) grid on

Output:
Computational
Mechanics-1

Example 9:

Write a MATLAB code to plot the three functions sinx, cosx and sinx+cosx on
Computational
Mechanics-1
different axes on the same figure using the subplot command.

Solution:

>> x=linspace(0,pi,200);
subplot(3,1,1)
plot(x,sin(x)) subplot(312)
plot(x,cos(x))
subplot(3,1,3)
plot(x,sin(x)+cos(x))
grid on

Output:
Computational
Mechanics-1
Construct the flowchart of an algorithm and generate the code that calculates the
area and perimeter of a circle, where the radius is entered by the user.

Radius = input('Please enter the radius\n');


Area = pi*(Radius^2);
Per= 2*pi*Radius;
disp(['Area of the circle is= ',num2str(Area)]);
fprintf('Perimeter of the circle is = % d\n',Per);
Output:
Please enter the radius
25
Area of the circle is= 1963.4954
Perimeter of the circle is = 1.570796e+02

Write a MATLAB code to find the first order and second order derivative of the
given function f=x3-7x2+6x-61.

Solution. The following code can be used to find the differentiation.

> syms x f=x.^3-7*x.^2+6*x-


61;
y=diff(f,x)
t=(diff(f,x,2))

Output:
y=

3*x^2 - 14*x + 6

t=

6*x - 14

You might also like