Coding a function to use the false position method to approximate roots

8 ビュー (過去 30 日間)
Aaron Atkinson
Aaron Atkinson 2020 年 3 月 1 日
コメント済み: Walter Roberson 2021 年 12 月 26 日
Im trying to code together a function to approximate roots using the false psotion method. My current issues is that I can't understand how I should go about plugging in a polynomial into matlab in a standard form.
function [root, fx, ea, iter] = falsePosition(func, xl, xu, es, maxit, varargin)
%falsePosition finds the root of a function using false position method
%please input the func value in this format ((8x^4)-(2x^2)+x)---->[8 0 -2 1]
if nargin <3
error('3 or more arguements required')
elseif nargin<4
es=.0001;
maxit=200;
end
plop=1;
for iter= 1:maxit
if plop==1;
root1= xl-(((polyval(func,xl))*(xu-xl))/((polyval(func,xu))-(polyval(func,xl))));
xu=root1;
plop=2;
ea=10000000;
elseif ea>es
root2= xl-(((polyval(func,xl))*(xu-xl))/((polyval(func,xu))-(polyval(func,xl))));
ea=((root2-root1)/root2)*100;
xu=root2;
else
break
end
end
I need to be able to plug in xu and xl into the equation, and I need to be able to plug the equation into the function in the form of ((3*x^3)+(8*x^2)*1) and not in the form of an array as [3 8 1]
  2 件のコメント
Aaron Atkinson
Aaron Atkinson 2020 年 3 月 1 日
I think I need to clarify, the primary issue I am having is translating the entered equation into something usable by matlab, how should I do this?
Walter Roberson
Walter Roberson 2021 年 12 月 26 日
((8x^4)-(2x^2)+x) should be [8 0 -2 1 0] not [8 0 -2 1] -- the 0 at the end is the constant coefficient.

サインインしてコメントする。

回答 (1 件)

darova
darova 2020 年 3 月 2 日
try matlabFunction
str = input('enter a function:\n');
f = matlabFunction(str);
  2 件のコメント
aisha azawe
aisha azawe 2021 年 12 月 25 日
f(x) = 10 * cos(x) + x^2 *sin(x) - 2
Walter Roberson
Walter Roberson 2021 年 12 月 26 日
f = @(x) 10 * cos(x) + x.^2 .* sin(x) - 2

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInterpolation についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by