I am having trouble finding the values for a certain variable which contains the "cosh(x)"

5 views (last 30 days)
% I am having problems with my variable Bn(n) because everytime I try to evaluate it, I get a bunch of zeros with only the first number being a real number. Is this how "cosh(x)" works? I am very confused and could use some help.
% P.S. My variable Ln(n) works fine, but I don't know what is wrong with Bn(n).
clear all, close all, nfig = 0;
%Data in Problem
S = 1200; To = 40; k = 0.16; HH = 16; H = 16*(0.0254); WW = 10; W = 10*(0.0254); Ly = H/2; Lx = W/2; Nx = 101; x = linspace(0, W, Nx)'; nmax = 50; tol = 0.001;
%Calculations of Ln and Bn
Ln = zeros(1,nmax); Bn = zeros(1,nmax);
for n = 1:nmax
Ln(n)= ((2*n-1)*pi)/(2*Lx);
Bn(n) = ((2*S)/(k*Lx))*(((-1)^n)./((Ln(n).^3).*cosh(Ln(n)*Ly)));
end

Accepted Answer

Roger Stafford
Roger Stafford on 26 Oct 2014
The function cosh(x) is defined as (exp(x)+exp(-x))/2 and therefore as x increases in size, the value of cosh(x) increases exponentially. I see that your values of Ln(n) are increasing up to approximately 1224 as n reaches nmax. This will make cosh(x) have +inf in the matlab "double" format. Since you are dividing by cosh(x), you will get zeros in Bn(n) for these larger values of n and very small values for many of the other values of n.
In other words, I am saying the behavior you are seeing is inherent in the numbers you are using. There is presumably nothing wrong with matlab's cosh(x) function - it is giving the values it is supposed to give. You can check it by substituting (exp(x)+exp(-x))/2 in place of cosh(x) and seeing if you get the same results.
By the way, you should be looking at your results with "format long", not the default "format short".
  4 Comments
Roger Stafford
Roger Stafford on 26 Oct 2014
Yes, your script looks correct. It is your observation of the results that is giving you trouble. The values of Bn will decrease rapidly and if you don't use high precision in displaying them - let's say you just use the default "format short" - then you will soon see the "bunch of zeros" that you described. As I previously recommended, try using "format long" or even better, use 'fprintf' with a high precision display, for exemple, %23.16e to fully display your values of Bn(n). You will see them getting smaller and smaller very rapidly because of the division by cosh and Ln^3. They will only become exactly zero when the cosh values have overflowed to infinity.
Yianni
Yianni on 26 Oct 2014
Excellent explanation! Yes that makes sense to me. I am going to try and use the fprintf command. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!