Unexpected zeros error in MATLAB in zeros function


, , ,

YouTube Comment: This MATLAB program gives me an error.  W=2.4; L=3; Delta=0.6; i=(W./Delta)+2; j=(L./Delta); T=zeros(i,j); . When I write 0.1 for Delta, there is an error for zeros statement.

Answer: When writing a new program, avoid using the semicolon as it suppresses the output. Write each line separately in a .m file and run the mfile. That way you would have noticed that “i” is turning out to be a real number. It shows up as 26.0000 but if you use format long statement, you will see that you get 25.999999999999996. You can round(i) and round(j) to the nearest integer. The reason “i” turns out to be 25.999999999999996 is because of roundoff error, as numbers and calculations get represented in binary format.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s