Why Does My MATLAB Code Generate a Singular Matrix Warning?

  • Context: MATLAB 
  • Thread starter Thread starter xpack
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion addresses a MATLAB code issue that generates a "Matrix is singular to working precision" warning. The user attempts to compute a probability surface using the equation Z=4*pi*(M./(2*pi*R*Y)).^(3./2)*X.^2.*exp(-M*X.^2)/(2*R*Y), which is incorrect. The correct formulation is exp((-M * X.^2) ./ (2 * R * Y). To facilitate debugging, it is recommended to disaggregate complex equations into simpler components.

PREREQUISITES
  • Understanding of MATLAB programming
  • Familiarity with matrix operations in MATLAB
  • Knowledge of mathematical modeling and probability distributions
  • Basic skills in debugging MATLAB code
NEXT STEPS
  • Learn how to use MATLAB's debugging tools effectively
  • Study MATLAB matrix operations and their implications on singular matrices
  • Explore mathematical modeling techniques in MATLAB
  • Investigate the use of element-wise operations in MATLAB for complex equations
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those involved in mathematical modeling, data visualization, and debugging code related to matrix computations.

xpack
Messages
37
Reaction score
0
I have this question:

http://img7.imageshack.us/img7/1399/52987960.png

And From this I created this:
Code:
delete g286x08.txt; diary g286x08.txt
clear; clc; close all; echo on
%
%Gilat 286/08
%

R=8.31;
M=0.032;
x=0:20:1000;
y=70:5:320;
[X Y]=meshgrid(x,y);

Z=4*pi*(M./(2*pi*R*Y)).^(3./2)*X.^2.*exp(-M*X.^2)/(2*R*Y);

surf(X,Y,Z)

xlabel('Molecules Speed(m/s)'); ylabel('Temperature'); zlabel('Probability')
%
echo off; diary off

And when I run it I get

Code:
%
%Gilat 286/08
%
R=8.31;
M=0.032;
x=0:20:1000;
y=70:5:320;
[X Y]=meshgrid(x,y);

Z=4*pi*(M./(2*pi*R*Y)).^(3./2)*X.^2.*exp(-M*X.^2)/(2*R*Y);
Warning: Matrix is singular to working precision. 
> In g286x08 at 13

surf(X,Y,Z)

xlabel('Molecules Speed(m/s)'); ylabel('Temperature'); zlabel('Probability')
%
echo off; diary off

And this image
http://img194.imageshack.us/img194/9489/30221576.jpg

I know there should be something plotted here. Can someone help me?
 
Last edited by a moderator:
Physics news on Phys.org
There is problem in this line:

Code:
Z=4*pi*(M./(2*pi*R*Y)).^(3./2)*X.^2.*[B]exp(-M*X.^2)/(2*R*Y)[/B];

it should be: exp((-M * X.^2) ./ (2 * R * Y)

To avoid mistyping, i think you should disaggregate your eq. (e.g A = 2 * R * Y ; B = (M ./ (pi * A)).^(3/2);). It is easier to debug. :) That is my exp in my sim course.
 
Ugh I hate when I do stupid stuff like this. Thank you very much!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
7
Views
3K
Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K