I need to be able to use a vector as an input to a function either as a variable or in form [ 1 2 ...N]

I need to be able to use a vector as an input to a function, but it can be either in vector form or as a variable name: here c0 and x are scalars. C and [1 2 3] are the vectors. They need to be interchangeable in the function as inputs. q = pmath(c0, C, x) or q = pmath(c0,[1 2 3],x) either way needs to work. Can this be done? finally I need to be able to produce p = c0 +c(1)x^1 + c(2)x^2+...c(n)x^n , can I do this with:
n = linspace(1,n,n);
c0 + sum(c(:).*x.^n(:));
?
Thank you. Hope its a little clearer with this statement.

9 Comments

DJ V's "Answer" moved here:
function out = poly_val( c0,Q,x )
%POLY_VAL Summary of this function goes here
% Detailed explanation goes here
[m,N]=size(Q);
cols = N;
if m >1
T= transpose(Q);
Q = T;
[m,N] = size(Q);
end
if m ==0 && cols ==0
out = c0;
end
if m ==1 && cols== 1
out=c0+Q*x;
end
if m==1 && cols>0
n=linspace(1,N,N);
out = c0+sum(Q(:).*x.^n(:));
end
end
When I try to debug this code. THe debugger skips over the first several lines. Its like its not there.
DJ V's "Answer" moved here:
My real problem at this time is that the program won't let me access the first several lines in the debugger. It's as if the debugger doens't see those lines. Its a major problem.
@DJ V: please stop writing comments as answers, because it actually makes it harder for us to keep track of what information you have given us. For the same reason please stop posting the same question multiple times.
"My real problem at this time is that the program won't let me access the first several lines in the debugger"
There are many ways that the debugging tools can be used. You have not told us how you are using the debugger, e.g. how you define breakpoints, if you step or continue to the next breakpoint, or something else. Do we have to guess, or use our crystal balls to know what you are doing? When you actually give useful information and describe what you are doing then we can give you a useful answer. We want to help you (that is why we are here), but you need to help us by giving more clues than just "It's as if the debugger doens't see those lines".
DJ V's "Answer" moved here:
Sorry, I'm new to this. I start the program with the following line after setting breakpoints (red circles) in the code. The debugger blast through the debugging code, and it seems to ignore the lines of code with the red circles. It gives me a result for P that is equal to the input matrix:
p = polyval(1, [1;1;1;1], 10)
@DJ V: please click the comment links to write comments You will see the comment links (e.g "comment on this Question" under each question and under every answer. I agree that this is a non-intuitive design, but unfortunately that is what we have to work with. Please do not keep creating answers that are not actually answers.
So you have some red breakpoints. How do you run the code? Do you call it from the command line, or something else? Is there any possibility that you have multiple functions with the same name stored in different directories?
On which lines do you set the breakpoints? [If you give the line numbers, please indicate to which line in the code you posted those numbers refer so we can be certain we're referring to the same lines.]
What are the exact inputs with which you call your function when it "blast[s] through the debugging code"?
Capture.jpg shows the entire screen with the problem. I launch the code from the command window, after setting it up to debug.
And do you plan on attaching capture.jpg? Insert it with the green and brown image frame icon.
One reason that it could skip past your breakpoints is if the function you are inserting breakpoints into is not the same function that you are running. You should check with
which -all poly_val
and make sure that the first one is the same file you are editing.

Answers (1)

why dont u declare c as a vector and then c0 = c(1) c1=c(2) etc because the eqaution is on the form of c0*x^0 +c1*x^1 etc so the power of x follows c
check this link about polynomials https://www.mathworks.com/help/matlab/ref/poly.html

1 Comment

The referenced web page is all about eigenvalues and characteristic polynomials which have nothing to do with my problem. Is it possible to response with the proper form of the Matlab code to accomplish the stated goal?

This question is closed.

Asked:

on 27 Nov 2016

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!