Comp Mechanics Antony
Comp Mechanics Antony
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.
>>
P=10;
n=2;
R=7;
T=1/2;
V=(n*R*T)/P
Output:
V=
0.7000
Computational
Mechanics-1
>> 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.
Write a MATLAB code to find the first order and second order derivative of the
given function f=x3-7x2+6x-61.
Output:
y=
3*x^2 - 14*x + 6
t=
6*x - 14