Basic Matlab Linearity Check Question
8 views (last 30 days)
Show older comments
I could really use some help in how to write the code to a test for linearity in matlab for say y=2x-2. I have looked all over the internet and can't find anything that explains how to write something for this and what the important points mean. I am new to matlab and I am still learning how to use it.
0 Comments
Answers (1)
Walter Roberson
on 16 Sep 2020
You can use coeffs() to probe.
coeffs will error if it thinks that the input is not a polynomial, so be prepared for that. It is plausible that coeffs might not be able to prove that an expression is polynomial for sufficiently complex expressions.
If the result from coeffs 'all' has more than two entries then the expression is not linear.
If the result from coeffs 'all' has one entry then the expression is constant in the variable.
If the result from coeffs 'all' is length two then the expression is linear in the variable.
You need to know which variable to ask for. For example A^2*x + B is linear in x and in B but not in A.
There are other approaches.
c0 = subs(y, x, 0)
c1 = subs(y, x, 1)
factor = c1-c0
c2 = subs(y, x, 2)
Now if c2 - c0 is not 2*factor to within roundoff then y is not linear in x. If c2 - c0 is not factor then y is not constant in x.
But the reverse is not true: 2*factor could be satisfied by something nonlinear. For example x*(x-1)*(x-2) would be 0 at 1 and at 2, and 2*0 = 0. So these kinds of probes of values cannot prove linearity.
0 Comments
See Also
Categories
Find more on Systems of Nonlinear Equations 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!