Expressing polynomial including a indefinite parameter

Hi All,
I know that the expression p=[1 -4 2] corresponds to p(x) = x^2 - 4x + 2.
I want to express p(x) = a*x^2 - 4x + 2-a
I would appreciate it if you told me how to do that.
Bests,
Cagdas

 Accepted Answer

One way.
p = @(x,a) polyval([a,-4,2-a],x);
Usage:
x=1; a=2;
p(x,a)
ans = -2

5 Comments

Hi Matt,
Thank you for your answer. But what I want to do is to keep the "a" as indefinite parameter. I do not want to evaluate the value of the polynomial for any value of "a".
Actually my final goal is to evaluate "a" as p(1) = a*x^2 - 4x + 2-a = 1 @ x = 1
Bests,
Cagdas
Maybe you mean,
p = @(x) @(a) polyval([a,-4,2-a],x);
This means p(x) will return a function of a for fixed x. Example,
p0=p(0); %returns a function of a at x=0
p0(7) %a=7, x=0
ans = -5
p0(10)%a=10,x=0
ans = -8
Comment by Cagas Akalin moved here
I think I made a confision but let me correct myself and ask my question as follows.
I understood that I can express a polynomial either by
p = @(x,a) polyval([a,-4,2-a],x);
or by
p = @(x) @(a) polyval([a,-4,2-a],x);
But what I am aiming to do is to give it a value only for x which returns a polynomial including the constant(indefinite) "a". Namely,
Let's assume my polynomial is
p(x,a) = 2*a*x^2 - 4x + 2-a
and my input is x = 1
and this returns
p(1,a) = 2*a*1^2 - 4*1 + 2-a = 2*a - 4 + 2 - a = a - 2
But what I am aiming to do is to give it a value only for x which returns a polynomial including the constant(indefinite) "a".
I maintain that that is what is achieved by doing as follows (updated for your new example).
Pxa = @(x) @(a) polyval([2*a,-4,2-a],x);
Now if we invoke px() at x=1,
p=Pxa(1); %x=1
then I claim that p(a) is the polynomial function a-2. We can check various inputs a to verify that this is true:
a=5; p(a)
ans = 3
a=2; p(a)
ans = 0
a=0; p(a)
ans = -2
Thank you Matt,
It works.
Bests,
Cagdas

Sign in to comment.

More Answers (1)

Possibly you mean that you want a coefficient vector as the output, rather than a polynomial in functional form.
syms a x
P = 2*a*x^2 - 4*x + 2-a
P = 
p=subs(P,x,1)
p = 
coefficients = sym2poly(p)
coefficients = 1×2
1 -2

3 Comments

Yes, what I was trying to implement was exactly that you guessed.
Dear Matt,
Since my question covers an different topic in the bigger picture I will pose it in an another question page.
Bests,
Cagdas
Yes, what I was trying to implement was exactly that you guessed.
I'm glad we converged, but please Accept-click the answer to indicate that your question was resolved.
Since my question covers an different topic in the bigger picture I will pose it in an another question page.
Yes, by all means.

Sign in to comment.

Categories

Asked:

on 21 Oct 2021

Edited:

on 21 Oct 2021

Community Treasure Hunt

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

Start Hunting!