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

조회 수: 115 (최근 30일)
Nicholas Morris
Nicholas Morris 2021년 8월 17일
댓글: Walter Roberson 2023년 4월 17일
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
vikas singh
vikas singh 2023년 4월 17일
Sir I need the value of q4 for each value of n=1:100.
Walter Roberson
Walter Roberson 2023년 4월 17일
Take the output of q4s in the code I show and do
output = subs(q4s, n, 1:100);

댓글을 달려면 로그인하십시오.

카테고리

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