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

45 次查看(过去 30 天)
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 个评论
Steven Lord
Steven Lord 2021-8-18
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 2021-8-18
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

请先登录,再进行评论。

采纳的回答

Nicholas Morris
Nicholas Morris 2021-8-18
Problem solved...Indexing errors.

更多回答(2 个)

Walter Roberson
Walter Roberson 2021-8-17
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 个评论
Walter Roberson
Walter Roberson 2021-8-18
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 2021-8-18
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?

请先登录,再进行评论。


vikas singh
vikas singh 2023-3-12
编辑:Walter Roberson 2023-3-12
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 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by