MATLAB is giving me 0's instead of really small numbers

108 views (last 30 days)
I don't have code to share, but MATLAB is outputing 0's instead carrying through small numbers. When I change the command window to format long, the calculation show non zero. However, when my code runs it is carrying these zeros through my calculations.
>> Calculation
ans =
0.0
>> Format long
>> Calculation
ans =
8.425293911978570e-07
How do I make matlab keep the small numbers?
  2 Comments
Steven Lord
Steven Lord on 18 Aug 2021
Can you show us a small segment of the code that appears to return 0's instead of the small numbers you expected?
How small is "really small" in this context? Smaller than realmin?
Nicholas Morris
Nicholas Morris on 18 Aug 2021
See the comments in the answer below.
The numbers are definitely not smaller than realmin. and I'm not really worried about the fact that my command window is displaying "0" for all values in my matrix, but more the fact that if I use the following:
>>find(array>0)
ans =
0×1 empty double column vector
so these numbers are actually zero.
I have a similar array in my code with small numbers that you can see display properly

Sign in to comment.

Accepted Answer

Nicholas Morris
Nicholas Morris on 18 Aug 2021
Problem solved...Indexing errors.

More Answers (2)

Walter Roberson
Walter Roberson on 17 Aug 2021
MATLAB is keeping the small numbers throughout your calculation.
I recommend that you put
format long g
at the top of your code and run again.
  3 Comments
Walter Roberson
Walter Roberson on 18 Aug 2021
Your code might be zeroing the entire array at each step.
But as well, "format long g" does not affect the variable browser. There is a drop-down above the variable name that can select the format for the variable browser; personally though I recommend setting Preferences -> Variables -> Default Array Format to "long g", which controls the default format for the variable browser.
Nicholas Morris
Nicholas Morris on 18 Aug 2021
I've already tried the "Preferences -> Variables -> Default Array Format to "long g"... : (
No luck.
What do you mean by my code "might be zeroing the entire array at each step"?? Why would it do this?

Sign in to comment.


vikas singh
vikas singh on 12 Mar 2023
Edited: Walter Roberson on 12 Mar 2023
I have written a code and it is giving me H3 and z2 zero. i dont know what is the problem.
clc;
clear all;
close all;
l=10000;
x=0:100:l;
t=[0;1;5;10;15;20;25];
H=30;
alpha=0.00384
Mu=2.8e-1;
D=31;
k=0.01132;
k1=l/H;
for n=1:100
a=2*H;
b=(1-(-1)^n*exp(-k*l))/((n*pi+(k^2*l^2)/(n*pi)));
c=1/(n*pi);
Cn(n)=a*(b-c);
end
s=0;
for n=1:100
d= sin(n*pi*x/l);
q= exp((-n^2*pi^2*t)/(Mu^2*l));
aa=Cn(n).*d.*q;
s=s+aa;
bb=(2*alpha*Mu^2*l^2)/(n*pi)^3;
s2=bb.*(1-q).*d;
end
J=(H+alpha*t).*x/l;
J1=alpha*t;
z1=s-s2-J+J1;
for n=1:100
syms X T
disp(n)
H0=(Cn(n)/(D-H)).*sin(n*pi*X).*exp(-n^2*pi^2*T/Mu)+(bb/(D-H)).*(1-exp(-n^2*pi^2*T/Mu)).*sin(n*pi*X) +(H/(D-H)+alpha*T*Mu*l/(D-H))*X+(H-alpha*T*Mu*l)/(D-H);
y=diff(H0*diff(H0,X),X);
Q=int(y*sin(n*pi*X),X,0,1);
P=int(Q*exp(n^2*pi^2*T/Mu),T,0,T);
Hn=-2*P/(exp(n^2*pi^2*T/Mu));
H1=Hn.*sin(n*pi*X);
for i=1:size(t,1)
H2=subs(H1,T,t/(Mu*l));
end
format long g
H3=subs(H2,X,x/l)
end
z2=double(((D-H)^2).*H3/l)
  22 Comments
vikas singh
vikas singh on 17 Apr 2023
Sir I need the value of q4 for each value of n=1:100.
Walter Roberson
Walter Roberson on 17 Apr 2023
Take the output of q4s in the code I show and do
output = subs(q4s, n, 1:100);

Sign in to comment.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!