Clear Filters
Clear Filters

Polynoom coëfficiënt from excel data

3 views (last 30 days)
In order to calculate a parameters in a 5th order Polynoom function, im using "fzero" in a function. The input data is from an excel list (code shown below). I would like to function to work with any data inputted from an excel sheet.
function y = Cp_REV02(~)
%clear
%clc
cp_data = xlsread('Data.xlsx', 'testing', 'P7:P44')
f = @(x)- 0.0006*x.^5 + 0.0104*x.^4 - 0.0602*x.^3 + 0.146*x.^2 - 0.108*x + 0.043 -cp_data;
init_guess = 3;
lambda = fzero(f, init_guess)
end
The error is shown below:
Operands to the and && operators must be convertible to logical scalar values.
Error in fzero (line 307) elseif ~isfinite(fx) ~isreal(fx)
Error in Cp_REV02 (line 9) lambda = fzero(f, init_guess)

Accepted Answer

Star Strider
Star Strider on 17 Oct 2015
To get the roots (zeros) of a polynomial, the easiest way is to use the roots function.
For instance:
cp_data = 1:5; % Create Data
for k1 = 1:length(cp_data)
p = [0.0006 +0.0104 -0.0602 +0.146 -0.108 +(0.043 - cp_data(k1))]; % Polynomial
r = roots(p); % All Roots
real_roots(:,k1) = r(imag(r)==0); % Real Roots
end
The fzero function only returns real roots, so I selected to retain only those. (If you want all of them, save ‘r’ instead of ‘real_roots’.) I used a cell array for ‘real_roots’ since the number of those may vary, depending on what ‘dp_data’ is in a particular iteration.
  8 Comments
Armando Marchena
Armando Marchena on 17 Oct 2015
I get it now. It also reads the input as variables and not as a single number It works indeed with my excel sheet. Thank you very much :)

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Functions in Microsoft Excel 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!