-2

m is either 0, 1, 2, or 3:

if m==0 
    afit0=afit(1);
elseif m==1
    afit0=afit(1);
    afit1=afit(2);
elseif m==2 
    afit0=afit(1);
    afit1=afit(2);
    afit2=afit(3);
elseif m==3 
    afit0=afit(1);
    afit1=afit(2);
    afit2=afit(3);
    afit3=afit(4);
end 

Is there a more concise way to write this code?

gnovice
  • 125,304
  • 15
  • 256
  • 359
John
  • 145
  • 5
  • 1
    Is `afit` a vector or function? Do you really need to create a bunch of separate numbered variables? Using vectors is a better approach. – gnovice Dec 12 '17 at 21:46
  • afit is a vector, but I need to name the individual elements of afit (they're coefficients of a polynomial). – John Dec 12 '17 at 21:54
  • If you explain the larger problem you're trying to solve, I'm quite certain there's a better approach. – gnovice Dec 12 '17 at 21:58
  • A function computes the vector afit, which is a column vector with possible lengths 1 through m, where m is inputted by the user. I've then got to name each of the elements in afit as afit0, afit1, ... , afitm. – John Dec 12 '17 at 22:08
  • @beaker I tried doing: For k=0:m afitk=afit(k+1) end however, this does not work. – John Dec 12 '17 at 22:09
  • Why not `a(1)`, `a(2)`, ..., `a(m)`? – beaker Dec 12 '17 at 22:09
  • Sorry, I'm not sure what you're referring to? – John Dec 12 '17 at 22:12
  • And I'm not sure why you're "renaming" the elements of the vector `afit`. It doesn't make sense. – beaker Dec 12 '17 at 22:13
  • I'm renaming the elements of the vector from a(1),a(2),...,a(m+1) to a0, a1,...,am as the polynomial is y=a0 + a1x + a2x^2 +...+amx^m, and I need to specifically state the values of the coefficients. – John Dec 12 '17 at 22:16
  • 1
    You could compute your polynomial using [vectorized operations](https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html) instead: `y = sum(afit(1:(m+1)).*x.^(0:m));` – gnovice Dec 12 '17 at 22:26
  • "Specifically state the values of the coefficients" Do you need to do this in the code (i.e. creating variables, which makes very little sense) or print to the screen in a user-readable format? I suspect the latter. Is this a homework exercise? If not, please describe the larger problem you're trying to solve. – Cris Luengo Dec 13 '17 at 03:08
  • I suppose I could just print to the command window. This is a homework exercise. So is there a more efficient way to code this if I just print to the command window? – John Dec 13 '17 at 11:12

1 Answers1

2

Any time you find yourself breaking a vector into separate variables in MATLAB, you're probably making more work for yourself. MATLAB is optimized for operations involving vectors and matrices, and using vectorization will often give you more efficient and concise code.

From your comments, it appears that you ultimately want to evaluate a polynomial y = a0 + a1*x + a2*x^2 + ... + am*x^m, where your coefficients a0 through am are the variables you are wanting to initialize from the column vector afit. A better alternative would be to use vectorized operations to compute your polynomial using afit directly:

y = sum(afit(1:(m+1)).*x.^(0:m).');

Here, we index afit based on the value of m and then multiply the resulting sub-vector element-wise by a value x raised element-wise to the power given in the column vector (0:m).'. The resulting vector of values is them summed using sum to get the result y.

As Ben points out, there is even a built-in function polyval that can perform this evaluation for you. You just have to flip the order of elements in the sub-vector indexed from afit:

y = polyval(flip(afit(1:(m+1)), 1), x);
gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 2
    `polyval` does this and allows the input to be a vector of values at which to evaluate the polynomial – Ben Voigt Dec 12 '17 at 23:04
  • @BenVoigt: Good point. I wanted to focus on the vectorization aspect, but it's worth mentioning. Added! – gnovice Dec 13 '17 at 04:22