How to Make Matlab Output Zero Coefficients in f=2x^2+1?

  • MATLAB
  • Thread starter motherh
  • Start date
  • Tags
    matlab
In summary, to output all the zero coefficients in Matlab when running coeffs(f), you would need to explicitly include them in the equation. Alternatively, you could start with f = [2 0 1] and work from there if it does not need to be done symbolically.
  • #1
motherh
27
0
Take f = 2x^2 + 1. When I run coeffs(f) in Matlab it outputs [2,1]. However I want it to output all the zero coefficients as well, for example [2 0 1] here. How do I do that?
 
Last edited:
Physics news on Phys.org
  • #2
motherh said:
Take f = 2x^2 + 1. When I run coeffs(f) in Matlab it outputs [2,1]. However I want it to output all the zero coefficients as well, for example [2 0 1] here. How do I do that?


My first thought would be that you'd need to have f = 2x^2 + 0x + 1 because with what you have f as right now, MATLAB doesn't know that the x coefficient is 0 if it's not explicitly typed.

Then again I'm no expert so there's a good chance I'm wrong, just a thought.
 
  • #3
Does this have to be done symbolically?

If not you should probably just *start* with f = [2 0 1] and work from there.
 

1. How do I make Matlab output zero coefficients in an equation?

To make Matlab output zero coefficients in an equation, you can use the "subs" function. This function allows you to substitute a specific value for a variable in an equation. For example, if you have the equation f=2x^2+1 and you want to make the coefficient of x^2 zero, you can use the command subs(f,x^2,0) to get the output f=1.

2. Can I use a loop to make Matlab output zero coefficients in multiple equations?

Yes, you can use a loop to make Matlab output zero coefficients in multiple equations. You can create a loop that iterates through a list of equations and uses the "subs" function to make the desired coefficient zero in each equation. This will save you time and effort compared to manually changing each equation.

3. Is there a way to make Matlab output only the coefficients of an equation?

Yes, you can use the "coeffs" function in Matlab to output only the coefficients of an equation. This function returns a vector containing the coefficients of the equation in descending order. You can then use this vector to manipulate or analyze the coefficients as needed.

4. What if I want to make Matlab output zero coefficients in a symbolic equation?

If you are working with symbolic equations in Matlab, you can use the "coeffsym" function to make outputting zero coefficients easier. This function allows you to specify the variable and the desired coefficient to be zero in the equation. For example, you can use the command coeffs(sym('2x^2+1'), 'x^2') to get the output 2.

5. Can I use a built-in function in Matlab to automatically make output zero coefficients in an equation?

Yes, there is a built-in function in Matlab called "polyval" that can be used to make outputting zero coefficients in an equation easier. This function evaluates a polynomial at a specific value. So, if you have the equation f=2x^2+1 and you want to make the coefficient of x^2 zero, you can use the command polyval([1 0 2], x) to get the output f=1.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
Back
Top