Polynoom coëfficiënt from excel data
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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)
採用された回答
Star Strider
2015 年 10 月 17 日
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 件のコメント
Armando
2015 年 10 月 17 日
Perhaps I did not explain my issue correctly. What I want to do is the following: I want to import excel data containing various values for cp_data. For each cp_data I would like to find the value for "x" in the polynomial equation below.
cp_data=-0.0006*x.^5+0.0104*x.^4-0.0602*x.^3+0.146*x.^2-0.108*x+0.043
Star Strider
2015 年 10 月 17 日
The fzero function determines the real roots (the value of x where y is zero), but is inefficient for polynomials with many roots, since you have to provide a new initial estimate of x for each root.
The roots function does that much more efficiently for polynomials. Your logic is correct — subtracting the constant (the y value you want to find the values of x that satisfy) from the polynomial will provide the values of x the meet that value (here cp_data) of y.
Armando Marchena
2015 年 10 月 17 日
I tried using the for loop as in your example, but to no avail. Would you happen to know what I'm doing wrong?
clear
clc
read_data = xlsread('Data.xlsx', 'testing', 'P7:P44');
cp_data = reshape(read_data,1,[]);
for ii = 0:length(cp_data);
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
This is the error I'm getting: Operands to the and && operators must be convertible to logical scalar values.
Error in fzero (line 307) elseif ~isfinite(fx) ~isreal(fx)
Star Strider
2015 年 10 月 17 日
The fzero function does not like complex values. Also, you are searching for one real root near 3 (because that is where you’ve told fzero to start looking) and for whatever reason (since I don’t know what ‘cp_data’ are), fzero is encountering a problem with your function in finding that zero-crossing. Your function appears to be correct, but fzero, as good as it is, is not as robust to your problem as you want it to be.
Also, you are not passing individual values of your ‘cp_data’ vector to your function in each loop, but instead the entire vector, and you are not saving the different results at each iteration, so lambda will have the results only of the last iteration. If you want to use ‘ii’ as an index vector, it has to begin with 1, not 0. All subscripts in MATLAB have to be positive integers. (Zero is not a positive integer.)
You really need to use my code as I posted it, because it actually will work in your application. If you want a specific root near 3 (or whatever value you want), post some values of ‘cp_data’ so I can run my code with it. (A bit of additional code should be able to isolate your single desired root, if you only want one. I just need some values of ‘cp_data’ to test my code, and to know the criteria to select the root you want.)
Armando Marchena
2015 年 10 月 17 日
編集済み: Armando Marchena
2015 年 10 月 17 日
The input cp_data would be the following: 0.110, 0.150, 0.190, 0.220, 0.270,
The answer for lambda (or "x" in the case of the polynoom) that I am expecting for each are (approx): 3.365, 4.165, 4.568, 4.767, 5.112,
For the sake of automation, the cp_data should be provided from a list.
Star Strider
2015 年 10 月 17 日
This works:
f = @(x,cpd)-0.0006*x.^5+0.0104*x.^4-0.0602*x.^3+0.146*x.^2-0.108*x+0.043-cpd; % Polynomial Function
cp_data = [0.110, 0.150, 0.190, 0.220, 0.270]; % Vector Of ‘cp_data’
for k1 = 1:length(cp_data)
lambda = fzero(@(x) f(x,cp_data(k1)), 3); % Call ‘fzero’
Result(k1,:) = [cp_data(k1) lambda];
end
Result =
0.11 3.2384
0.15 4.1127
0.19 4.5425
0.22 4.7804
0.27 5.1016
I added a variable for ‘cp_data’ in the argument list for ‘f’, then called it with that value in each iteration (call to fzero) to calculate ‘lambda’.
I added the ‘Result’ matrix to save the values of ‘cp_data’ in the first column, and the corresponding values of ‘lambda’ in the second column.
Armando Marchena
2015 年 10 月 17 日
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 :)
Star Strider
2015 年 10 月 17 日
My pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で R Language についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
