Why Does My MATLAB Code Generate a Singular Matrix Warning?

  • Thread starter Thread starter xpack
  • Start date Start date
AI Thread Summary
The discussion revolves around a MATLAB code snippet intended to plot a surface graph based on a mathematical model. The user encounters a warning indicating that the matrix is singular, which suggests an issue with the calculations in the line defining the variable Z. The problematic equation is identified, and a suggestion is made to rewrite it for clarity and easier debugging. Specifically, it is recommended to break down the equation into smaller components to avoid errors and improve readability. The user expresses frustration over the mistake and appreciates the guidance provided.
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
Views
4K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
4K
Replies
9
Views
3K
Replies
1
Views
2K
Replies
1
Views
369
Back
Top