Tabulating a Function with exponential values and using fprintf

2 vues (au cours des 30 derniers jours)
Ryan Phan
Ryan Phan le 18 Fév 2021
Modifié(e) : Stephen23 le 19 Fév 2021
I want to tabulate my function. However, when I do, all my y values are zeros since my numbers are really small from the range of 10^-18 to 10^-14. My prof commented how I should use fprintf instead of disp and that fprintf statement having z(j) and y(j) as the varialbes print. How can I make a table where my table will show y-values with an exponential notation.
----------------------------------------------
%script to call Bohr_Model_RJP
%prepare for loop
npts=118;
dz = 118/(npts-1);
disp( 'z' )
n=1;
%loop
for j=1 : npts
z(j) = dz*(j-1);
y(j) = zeff(z(j),n);
disp ([ z(j), y(j) ])
end
function E = zeff(z,n)
%n-value represents an electron circular-orbit around a nucleus; the higher the n-value, the farther the orbit%;
%z-value represents the atomic number of an element, randomly generated from 0 to 118 because there are 118 elements on the periodic table;
R= 2.178*10^-18;
%R-value represents the Rydberg constant which is measured in Joules
E=-(R*z^2/n^2);
% Formula calculating the energy a nucleus exerts on an electron
%value is negative because the nucleus pulls the electron towards it.
end

Réponses (1)

Stephen23
Stephen23 le 19 Fév 2021
Modifié(e) : Stephen23 le 19 Fév 2021
The loop is not required. By vectorizing the function you can simplify your code.
npts = 118;
n = 1;
z = linspace(0,npts,npts); % odd, but exactly the same as what you have.
y = zeff(z,n);
fprintf('%+.4e %+.4e\n',[y;z])
-0.0000e+00 +0.0000e+00 -2.2154e-18 +1.0085e+00 -8.8616e-18 +2.0171e+00 -1.9939e-17 +3.0256e+00 -3.5446e-17 +4.0342e+00 -5.5385e-17 +5.0427e+00 -7.9754e-17 +6.0513e+00 -1.0855e-16 +7.0598e+00 -1.4178e-16 +8.0684e+00 -1.7945e-16 +9.0769e+00 -2.2154e-16 +1.0085e+01 -2.6806e-16 +1.1094e+01 -3.1902e-16 +1.2103e+01 -3.7440e-16 +1.3111e+01 -4.3422e-16 +1.4120e+01 -4.9846e-16 +1.5128e+01 -5.6714e-16 +1.6137e+01 -6.4025e-16 +1.7145e+01 -7.1779e-16 +1.8154e+01 -7.9976e-16 +1.9162e+01 -8.8616e-16 +2.0171e+01 -9.7699e-16 +2.1179e+01 -1.0722e-15 +2.2188e+01 -1.1719e-15 +2.3197e+01 -1.2761e-15 +2.4205e+01 -1.3846e-15 +2.5214e+01 -1.4976e-15 +2.6222e+01 -1.6150e-15 +2.7231e+01 -1.7369e-15 +2.8239e+01 -1.8631e-15 +2.9248e+01 -1.9939e-15 +3.0256e+01 -2.1290e-15 +3.1265e+01 -2.2686e-15 +3.2274e+01 -2.4126e-15 +3.3282e+01 -2.5610e-15 +3.4291e+01 -2.7139e-15 +3.5299e+01 -2.8711e-15 +3.6308e+01 -3.0329e-15 +3.7316e+01 -3.1990e-15 +3.8325e+01 -3.3696e-15 +3.9333e+01 -3.5446e-15 +4.0342e+01 -3.7241e-15 +4.1350e+01 -3.9079e-15 +4.2359e+01 -4.0963e-15 +4.3368e+01 -4.2890e-15 +4.4376e+01 -4.4862e-15 +4.5385e+01 -4.6878e-15 +4.6393e+01 -4.8938e-15 +4.7402e+01 -5.1043e-15 +4.8410e+01 -5.3192e-15 +4.9419e+01 -5.5385e-15 +5.0427e+01 -5.7622e-15 +5.1436e+01 -5.9904e-15 +5.2444e+01 -6.2230e-15 +5.3453e+01 -6.4601e-15 +5.4462e+01 -6.7016e-15 +5.5470e+01 -6.9475e-15 +5.6479e+01 -7.1978e-15 +5.7487e+01 -7.4526e-15 +5.8496e+01 -7.7118e-15 +5.9504e+01 -7.9754e-15 +6.0513e+01 -8.2435e-15 +6.1521e+01 -8.5160e-15 +6.2530e+01 -8.7929e-15 +6.3538e+01 -9.0742e-15 +6.4547e+01 -9.3600e-15 +6.5556e+01 -9.6502e-15 +6.6564e+01 -9.9449e-15 +6.7573e+01 -1.0244e-14 +6.8581e+01 -1.0547e-14 +6.9590e+01 -1.0855e-14 +7.0598e+01 -1.1168e-14 +7.1607e+01 -1.1485e-14 +7.2615e+01 -1.1806e-14 +7.3624e+01 -1.2131e-14 +7.4632e+01 -1.2462e-14 +7.5641e+01 -1.2796e-14 +7.6650e+01 -1.3135e-14 +7.7658e+01 -1.3478e-14 +7.8667e+01 -1.3826e-14 +7.9675e+01 -1.4178e-14 +8.0684e+01 -1.4535e-14 +8.1692e+01 -1.4896e-14 +8.2701e+01 -1.5262e-14 +8.3709e+01 -1.5632e-14 +8.4718e+01 -1.6006e-14 +8.5726e+01 -1.6385e-14 +8.6735e+01 -1.6768e-14 +8.7744e+01 -1.7156e-14 +8.8752e+01 -1.7548e-14 +8.9761e+01 -1.7945e-14 +9.0769e+01 -1.8346e-14 +9.1778e+01 -1.8751e-14 +9.2786e+01 -1.9161e-14 +9.3795e+01 -1.9575e-14 +9.4803e+01 -1.9994e-14 +9.5812e+01 -2.0417e-14 +9.6821e+01 -2.0845e-14 +9.7829e+01 -2.1277e-14 +9.8838e+01 -2.1713e-14 +9.9846e+01 -2.2154e-14 +1.0085e+02 -2.2599e-14 +1.0186e+02 -2.3049e-14 +1.0287e+02 -2.3503e-14 +1.0388e+02 -2.3962e-14 +1.0489e+02 -2.4425e-14 +1.0590e+02 -2.4892e-14 +1.0691e+02 -2.5364e-14 +1.0791e+02 -2.5840e-14 +1.0892e+02 -2.6321e-14 +1.0993e+02 -2.6806e-14 +1.1094e+02 -2.7296e-14 +1.1195e+02 -2.7790e-14 +1.1296e+02 -2.8288e-14 +1.1397e+02 -2.8791e-14 +1.1497e+02 -2.9299e-14 +1.1598e+02 -2.9810e-14 +1.1699e+02 -3.0326e-14 +1.1800e+02
function E = zeff(z,n)
%n-value represents an electron circular-orbit around a nucleus; the higher the n-value, the farther the orbit%;
%z-value represents the atomic number of an element, randomly generated from 0 to 118 because there are 118 elements on the periodic table;
R = 2.178*10^-18;
%R-value represents the Rydberg constant which is measured in Joules
E = -(R*z.^2/n^2); % !!! vectorized power operation !!!
% Formula calculating the energy a nucleus exerts on an electron
%value is negative because the nucleus pulls the electron towards it.
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by