A newbie question: quiver failed to draw direction field of differential equation with 'Warning: Matrix is singular to working precision'

1 view (last 30 days)
Dear All:
I am using web version MatLab to try to draw direction field for 1st order differential equation by using quiver function. Sometimes the direction field was successfully plotted, but sometimes failed, with warning message: "Warning: Matrix is singular to working precision".
For example, this code below susscessfully plotted the direction field of DE(y'=y^2):
% y'=y^2
[T, Y]=meshgrid(-2:0.2:2, -3:0.5:3)
S=Y.^2;
L=sqrt(1+S.^2);
quiver(T, Y, 1./L, S./L, 0.3), axis tight
xlabel 't', ylabel 'y'
title 'Direction Field for dy/dt = y^2'
However, this code failed when I tryied to plot y'=t^2/(1-y^2):
% y'=t^2/(1-y^2)
[T, Y]=meshgrid(-3:0.1:3, -3:0.1:3)
S=T.^2/(1. - Y.^2);
L=sqrt(1+S.^2);
quiver(T, Y, 1./L, S./L, 0.3), axis tight
xlabel 't', ylabel 'y'
title 'Direction Field for dy/dt = t^2/(1-y^2)'
Ths plot is blank, with message: "Warning: Matrix is singular to working precision". The expected direction field is attached as a picture.
Thank you very much for your help. I greatly appreciate it.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Feb 2020
[T, Y]=meshgrid(-3:0.1:3, -3:0.1:3)
T and Y are going to be equal size matrices after that.
S=T.^2/(1. - Y.^2);
Each element of T is squared individually. Each element of Y is squared individually and that is subtracted from 1.
Now you take the two matrices and you ask to do least squared fitting of the two together, very similar to what you would get from
(T.^2) * pinv(1. - Y.^2)
where the * operator here is algebraic matrix multiplication, not element-by-element multiplication.
I would suggest to you that you do not want to do least-squared fitting of the two matrices, and that what you want instead is to do element-by-element division. Element-by-element division is the ./ operator, not the / operator.
  1 Comment
ryecatcher
ryecatcher on 13 Feb 2020
Dear Sir:
Thank you very much for your excellent informational reply. I have added a dot in the code and it is working now:
previous wrong code:
S=T.^2 / (1 - Y.^2);
Now I have added a dot right before '/':
S=T.^2 ./ (1 - Y.^2);
The direction field comes out correctly. Thank you again!

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!