New to MATLAB - trying to write bisection method?

Hello, I'm brand new to MATLAB and am trying to understand functions and scripts, and write the bisection method based on an algorithm from our textbook. However, I'm running into problems. Could anyone help me please?
Here is my code:
function [f] = Bisection(a,b,Nmax,TOL)
f = x^3 - x^2 + x;
i=1;
BisectA=f(a);
while i <= Nmax
p=a+(b-a)/2;
BisectP=f(p);
if BisectP == 0 || (b-a)/2 < TOL
disp('p');
end
i=i+1;
if BisectA*BisectP > 0
a=p;
BisectA=BisectP;
else
b=p;
end
end
disp('Method failed after num2str(Nmax) iterations, Nmax=', Nmax);
Thanks.

3 件のコメント

Walter Roberson
Walter Roberson 2013 年 12 月 15 日
What problems are you running into? Are you getting an error message? If so then at which line, and under what circumstances?
youcef mokrane
youcef mokrane 2020 年 11 月 9 日
編集済み: Walter Roberson 2020 年 11 月 9 日
x=4:4.7
f=tan(x)-x
a=4
b=4.7
fa=tan(a)-a
fb=tan(b)-b
n=1
n0=5000
while n<5000
p=(a+b)/2
fp=tan(p)-p
n=n+1
if fa*fp>0
a=p
else
b=p
end
end
Walter Roberson
Walter Roberson 2020 年 11 月 9 日
Why are you bothering to do x=4:4.7 ? The default increment for the colon operator is 1, so 4:4.7 is the same as 4:1:4.7 which is just going to be 4 .
Why are you assigning to n0 when you do not use it?

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

 採用された回答

Walter Roberson
Walter Roberson 2013 年 12 月 15 日
編集済み: Walter Roberson 2013 年 12 月 16 日

1 投票

Your line
f = x^3 - x^2 + x;
does not define a function. Try
f = @(x) x^3 - x^2 + x;

10 件のコメント

AM
AM 2013 年 12 月 16 日
Hi, I tried that but it gave me an "unexpected MATLAB expression" error. I then tried f(x) = x^3 - x^2 + x;
but I received an "undefined function or variable 'x' " message.
I have no idea what's going on?!
Walter Roberson
Walter Roberson 2013 年 12 月 16 日
Sorry somehow my fingers missed the @. I have corrected above.
Nick Zarantonello
Nick Zarantonello 2015 年 4 月 9 日
You need to use dot multiplication
SADIA RASHEED
SADIA RASHEED 2016 年 9 月 29 日
編集済み: Walter Roberson 2016 年 9 月 29 日
function [f] = Bisection(a,b,Nmax,TOL)
f = @(x) x^3 - x^2 + x;
i=1;
BisectA=f(a);
while i <= Nmax
p=a+(b-a)/2;
BisectP=f(p);
if BisectP == 0 || (b-a)/2 < TOL
disp('p');
end
i=i+1;
if BisectA*BisectP > 0
a=p;
BisectA=BisectP;
else
b=p;
end
end
disp('Method failed after num2str(Nmax) iterations, Nmax=', Nmax);
%%%%but it gives an error function [f] = Bisection(a,b,Nmax,TOL)
|
Error: Function definitions are not permitted in this context.
plz help
Walter Roberson
Walter Roberson 2016 年 9 月 29 日
You need to store the code in a file named Bisection.m
It is not permitted to define functions from the MATLAB command line.
In all MATLAB versions up to R2016a, it was also not permitted to define functions in "script" files -- that is, files that do not start with the word "function" or "classdef". In R2016b that is now permitted, though.
SADIA RASHEED
SADIA RASHEED 2016 年 9 月 30 日
okay.Thanks
Silverio Jr Magday
Silverio Jr Magday 2017 年 6 月 15 日
Error in ==> Bisection at 4 BisectA=f(a);
what possible correction I can make?
Walter Roberson
Walter Roberson 2017 年 6 月 15 日
Silverio Jr Magday:
You can make the correction of going down to the command line and calling the function by name, passing in appropriate valeus for a, b, Nmax, and TOL, such as
Bisection(-8, 14, 207, 1e-10)
You attempted to run the code by clicking on "Run", which is the same as if you had gone to the command line and commanded
Bisection
without passing in anything. Then when the code reached the first line in which it needed one of the parameters, the code failed because there was no parameter there.
A_J Khan
A_J Khan 2017 年 9 月 18 日
編集済み: Walter Roberson 2017 年 9 月 18 日
function p = bisection(f,a,b)
|
Error: Function definitions are not permitted in this
context.
I have this error with above code....???
Walter Roberson
Walter Roberson 2017 年 9 月 18 日
You can never define a function at the command prompt: you have to store a function inside a .m file; in this case, bisection.m
In versions up to R2016a you cannot store a function inside a script file (a .m file that does not start with the word "function" or "classdef"). That changed in R2016b.

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

その他の回答 (1 件)

Frank Cano
Frank Cano 2019 年 4 月 15 日

2 投票

Here are some Bisection method examples

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

質問済み:

AM
2013 年 12 月 15 日

コメント済み:

2020 年 11 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by