0% found this document useful (0 votes)
40 views29 pages

MATLAB FDP Day 2

Uploaded by

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

MATLAB FDP Day 2

Uploaded by

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

Matlab Programming For Mechanical

Engg. Research
Day 2
By
MD Irfan Ali
3D Plots
Following are the most used 3D plot commands in Matlab:
• plot3: Generates 3-D line graph
• mesh: Generates 3-D mesh plot
• surf: Generates 3-D surface plot

To use 3D plots z=f(x,y)

Example:

Note:

• Top-view: - view (0, 90)


• Side-view(x,z): - view (0, 0)
• Side-view(y,z): - view (90, 0)
3D Plots (plot3)

clc
clear all
close all
x=-3:0.1:3;
y=x;
z=(x.^2+y.^2);
figure;
plot3(x,y,z)
grid on
figure;
plot3(x,y,z)
view (0,0)
grid on
3D Plots(mesh)
clc
clear all
close all
x=-3:0.1:3;
y=x;
[X,Y]=meshgrid(x,y);
z=(X.^2+Y.^2);
figure;
surf(x,y,z)
grid on
figure;
surf(x,y,z)
view (0,0)
grid on
3D Plot
Try surf plot for the below function:
z
3D Plot
Try surf plot for the below function:
z
clc
clear all
close all
x=-3:0.1:3;
y=x;
[X,Y]=meshgrid(x,y);
z=exp((-X.^2)/2)+exp((-Y.^2)/2);
figure;
surf (x,y,z)
grid on
figure;
surf (x,y,z)
view (90,0)
grid on
Curve Fitting
• Steps:
1) Plot the data using the following command
plot(x,y,’o’)
2) From the figure menu bar click ‘Tools’ and select ‘Basic Fitting’
from the drop-down menu.
3) Fit an appropriate curve and find the fitting constants.
Curve Fitting
• Step 1: Step2:
x=linspace(1,10,10);
y=x.^2;
plot(x,y,'o’)

• Step 3:
‘If’ condition checking
The 'if' statement is a conditional statement in programming; if the 'if' condition is satisfied, only the statements
following the 'if' statement are executed.

Syntax1:
if condition -------% ‘condition’=checking statement.
statements --------------------% these lines are executed, if ‘if’ condition is true.
else ----% if ‘if’ condition is not true, then, ‘else’ statements are executed.
statements
end--------------------------% stops the execution of the code.

Syntax2:
if condition
statements
elseif condition ---------------% ‘elseif’ also behaves like ‘if’, and when ‘if’ condition is not true, then the editor
jumps to ‘elseif’ to check the condition.
statements
else
Statements
end
‘If’ condition checking

Water at 10 oC flows through a pipe of length 3m and diameter 1cm. The velocity at the inlet of the pipe is
0.4m/s. Using ‘if’ code to find whether the flow is laminar or turbulent. Take density of water to be 1000 kg/m 3,
and kinematic viscosity to be 1.306X10-6 m2/s.
Background of Fluid Mechanics:
'ReynoldsNumber(Re)=Velocity*(Diameterof pipe)/(Kinematic Viscosity)'
Re<2100 flow is Laminar flow
Re>=2100 is Turbulent flow
‘If’ condition checking
Water at 10 oC flows through a pipe of length 3m and diameter 1cm. The velocity at the inlet of the pipe is 0.4m/s.
Using ‘if’ code to find whether the flow is laminar or turbulent. Take density of water to be 1000 kg/m 3, and
kinematic viscosity to be 1.306X10-6 m2/s.
Background of Fluid Mechanics:
'ReynoldsNumber(Re)=Velocity*(Diameterof pipe)/(Kinematic Viscosity)'
Re<2100 flow is Laminar flow
Re>2100 is Turbulent flow.
clc
clear all
V=input('enter the velocity of the water in m/s \n');
D=input('enter the diameter of the pipe in meters \n');
% kinematic viscosity
KV=1.306*(10^-6);
% calculating ‘Re’
Re=V*D/KV;
if Re<2100
display(Re)
display('the flow is laminar')
else
display(Re)
display('the flow is turbulent')
end
‘for’ loop
Syntax: for index_variable=initial_value:final_value
Or
for index_variable=initial_value:incrementing_step:final_value
statements
end

Example:
clc
clear all
for i=1:1:10
fprintf('you are in for loop and step is % 2.1f \n',i)
end
‘for’ loop
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10

0.1 m/s 0.2 m/s 0.3 m/s 0.4 m/s 0.5 m/s 0.6 m/s 0.7 m/s 0.8 m/s 0.9 m/s 1 m/s

Use ‘for’ loop to find the ‘Re’ and type of flow for different velocities given in the table.
‘for’ loop
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

clc
clear
format long
V=.1:0.1:1;
D=input('enter the diameter of the pipe in meters \n');
KV=1.306*(10^-6);
for i=1:1:10
Re=V(i)*D/KV;
if Re<2100
I=[V(i);Re];
fprintf('the flow is laminar for the velocity of water %2.2f m/s and the Re is
%6.6f \n',I)
else
I=[V(i);Re];
fprintf('the flow is turbulent for the velocity of water %2.2f m/s and the Re is
%6.6f \n',I)
end
end
Function File: User Defined Function
Syntax: function [output variables]=function_name(input variables)

Where h=convective heat transfer coefficient in W/m 2K

k= thermal conductivity of the material in W/mK

function[bi]=biot(r,h,k)---%[bi]=output variable, ‘biot’=function_name, (r,h,k)=input variables.


V=4/3*pi*r^3; -------------% calculating volume of sphere.
A=4*pi*r^2; ----------------% calculating surface area of sphere.
lc=V/A; ---------------------% calculating characteristic length of sphere.
bi=h*lc/k; -------------------% calculating ‘Biot number’ of sphere.
end ---------------------------% ends function ‘biot’.
Lumped Capacitance Problem
An aluminum sphere of radius 0.0786 meters initially at a temperature of 250 oC is suddenly immersed in a fluid
at 17oC. The convective heat transfer coefficient is 60W/m 2K. Estimate the time required to cool the aluminum
to 100oC. Take density of aluminum to be 2700kg/m 3, specific heat to be 900J/kgK, and thermal conductivity to
be 205W/mK.
Time required for cooling in lumped capacitance method is given by the following expression:

Where,
= time in seconds
= final temperature of sphere
= fluid temperature
= initial temperature of sphere
= convective heat transfer coefficient
= density of the material in kg/m3
= specific heat in J/kgK
= radius of sphere
Lumped Capacitance Problem
% calling user defined function biot.
clc
clear all
bi=biot(0.0786,58,205);
display(bi)

% condition checking, in this problem it is true i.e. bi<0.1.


if bi<0.1
disp('solving by lumped capacitance method')
Tf=input('enter the final temperature of the sphere \n');
Tfl=input('enter the fluid temperature \n');
To=input('enter the initial temperature of the sphere \n');
r=0.0786;h=60;density=2700;cp=900;
T=log((Tf-Tfl)/(To-Tfl));
fract=-((h*3)/(density*cp*r));
time=(T/fract);
fprintf('the time required is %f seconds \n',time)
else
disp('Temperature is a function of space too!')
end
Differentiation
Differentiation can be performed using the ‘diff’ function of matlab.

Steps:
1) Use ‘syms’ command to create variables
2) Define the equation to be differentiated
3) Use ‘diff’ function to find the derivative
Syntax:
diff(dependent_variable,independent_variable,derivative_order)
Differentiation (Example)
• If

clc
clear all
syms x y
y=4*x^3+2*x^2+3*x+6
pretty(y)
y1=diff(y,x,1)
y2=diff(y,x,2)
Differentiation (Example)

If
clc
clear all
syms x y
y=x+exp(x^2)
pretty(y)
y1=diff(y,x,1)
y2=diff(y,x,2)
Integration
Integration can be performed using the ‘int’ function of matlab.
Steps:
1) Use ‘syms’ command to create variables
2) Define the equation to be integrated
3) Use ‘int’ function to find the derivative
Syntax:
int(dependent_variable,independent_variable)
Integration (Example)
Find .

clc
clear all
syms x y
y=4*x^2+5*x+6
pretty(y)
int(y,x)
finsol=int(y,x,1,2)
‘trapz’ function for integration
% Define the function to be integrated (e.g., f(x) = x^2)
f = @(x) x.^2;
% Set the limits of integration
a = 0;
b = 1;
% Choose the number of points for approximation
n = 100;
% Define the x-values and y-values
x_values = linspace(a, b, n+1);
y_values = f(x_values);
% Use the trapz function to compute the integral
result = trapz(x_values, y_values);
% Display the result
fprintf('Approximate integral using trapz function: %f\n', result);
Solving ordinary differential equations using ‘dsolve’ function

Solving an IVP of the form

Syntax: dsolve(‘differential equation’, ‘initial condition’, ‘independent variable’)

Note: ‘dsolve’ function will take ‘t’ as the default independent variable, and .
Solving an IVP
Solve the following IVP subject to an initial condition .
clc
clear all
dsolve('Dy-4*t-y/2=0', 'y(0)=-1','t')
Solving an IVP
A stainless-steel cylindrical rod of outer diameter 2 cm which is initially at a temperature of 420 oC is suddenly
immersed in a liquid which is at 110oC. The convective heat transfer coefficient is 80 W/m 2K. Take density of
stainless steel as 7800 kg/m3 and specific heat to be 440 J/kg K. Determine the temperature of the rod after 3 minutes
of immersion.
Heat Transfer Knowledge:
Governing Differential equation:
Where T= Temperature at any time ‘t’
Tf = Temperature of the fluid
h= Convective heat transfer coefficient
A= surface area of the cylindrical rod
V= Volume of the cylindrical rod
ρ = Density of the cylindrical rod
C= Specific heat of the cylindrical rod
Solving an IVP
A stainless-steel cylindrical rod of outer diameter 2 cm which is initially at a temperature of 420 oC is suddenly immersed
in a liquid which is at 110oC. The convective heat transfer coefficient is 80 W/m2K. Take density of stainless steel as 7800
kg/m3 and specific heat to be 440 J/kg K. Determine the temperature of the rod after 3 minutes of immersion.
Heat Transfer Knowledge:
Governing Differential equation:
Where T= Temperature at any time ‘t’
Tf = Temperature of the fluid
h= Convective heat transfer coefficient
A= surface area of the cylindrical rod
V= Volume of the cylindrical rod
ρ = Density of the cylindrical rod
C= Specific heat of the cylindrical rod
Matlab code:
clc
clear all
dsolve('DT+0.004662*T-0.51282=0', 'T(0)=420', 't')
1-D Steady State Heat Conduction (BVP)
(Governing Equation)

; Boundary conditions: T.
1-D Steady State Heat Conduction (BVP)
(Governing Equation)

; Boundary conditions: T.

Matlab Code:
clc
clear all
dsolve('D2y=0', 'y(0)=15', 'y(1)=50', 't')

You might also like