MATLAB Energy Unit Conversion Script
MATLAB Energy Unit Conversion Script
The conversion factors between units are: 1J = 0.738 ft-lb = 0.239 cal = 6.24*1018eV.
En utilisant votre programme, déterminer a) 325J en ft-lb, b) 432 cal en Joules, c) 6.8 eV en
calories.
Use the command 'disp' to display the result on the screen (use the help!)
Enter the value of energy, its unit, and the new unit
% désirée:
V_in = input('quantité d''énergie à convertir: ');
Ein_unit = input('entrer son unité(J, ft-lb, cal ou eV): ','s');
Eout_unit = input('enter the desired unit (J, ft-lb, cal or ...
eV): ','s');
case 'ft-lb'
E_Joule = V_in/0.738;
case 'cal'
E_Joule = V_in/0.239;
case 'eV'
E_Joule = V_in/(6.24*10^18);
otherwise
error = 1; %l’erreur est vraie
end
1/7
MATLAB Course UNIL-FGSE - Winter 2009-2010
III. Programming Principles - Solution
Auteur : Alexandre Loye
switch Eout_unit
case 'J'
E_new = E_Joule;
case 'ft-lb'
E_new = E_Joule*0.738;
case 'cal'
E_new = E_Joule*0.239;
case 'eV'
E_new = E_Joule * 6.24e18;
otherwise
error = 1;
end
Réponse :
2/7
MATLAB Course UNIL-FGSE - Winter 2009-2010
III. Programming Principles - Solution
Auteur : Alexandre Loye
for k=1:n
S = S + (-1)^k*k/2^k;
end
Réponses :
∞ k 2k
• The function cos(x) can be written as a Taylor series: cos (x) = ∑ (−1)(2k)!x .
k =0
The input arguments are the angle x in degrees (note: Matlab is in radians) therefore
let the number n of terms in the series. Use the for - end structure to calculate
cos(210°) with n = 3 and n = 8. Compare your answer with the function cos(x)
implemented in Matlab.
for k=0:n
S = S + (-1)^k*xr^(2*k) / factorial(2*k);
end
3/7
MATLAB Course UNIL-FGSE - Winter 2009-2010
III. Programming Principles - Solution
Auteur : Alexandre Loye
Réponses :
∞ n
• The function f(x) = excan be written in a Taylor series: ex= ∑ xn! .
n=0
Write a program that determines exby adding the terms of the Taylor series
until the absolute value of the last included term is < 10-4To do this, use the
while loop - limit the number of iterations to 20. If after 20 iterations, the
the incremented value is not less than 10-4, the program stops indicating a
message ( ex. ‘valeur eximprecise'). Give your answer for e3, e-4and e18.
define the terms of the Taylor series by looking at the first ones
%terms:
% exp(x) = 1 + x + x^2/2! + x^3/3!
expX = 1;
use the while command to calculate the n-th term of the series
%et add it to the sum.
4/7
MATLAB Course UNIL-FGSE - Winter 2009-2010
III. Programming Principles - Answer Key
Auteur : Alexandre Loye
Answer:
3. Conditions / loops
Create using a combination of loops and conditions the terrain profile as well as its
soil thickness below:
The coordinates of the land can be specified using the "for - end" structure.
Exercise:
• create in the form of a vector the coordinates in x and z and gather them under a
even using the structure "for - end" (see advice below!).
5/7
MATLAB Course UNIL-FGSE – Winter 2009-2010
III. Programming Principles - Solution
Auteur : Alexandre Loye
figure
plot(xzdata(:,1), xzdata(:,2), '-r', 'LineWidth', 2)
axis([min(x) max(x) 0 30]);
The thickness of the ground (normal to the ground) is 2 meters along its entire length.
The thickness of the soil is proportional to the slope, such that: S = soil thickness * cos(slope)
% separate the xzdata matrices into two matrices xdata and zdata
%NB: when working with loops, it is sometimes
It is wise to preallocate memory to optimize speed.
% of calculation by declaring the variables in advance.
xdata = zeros(11,1);
zdata = zeros(11,1);
for i = 1:11
xdata(i) = xzdata(i,1);
zdata(i) = xzdata(i,2);
end
6/7
MATLAB Course UNIL-FGSE - Winter 2009-2010
III. Programming Principles - Solution
Auteur : Alexandre Loye
graphical representation
figure
plot(X,Z,X,S,'LineWidth',1.5)
axis([xmin xmax 0 30])
7/7