myFunction =@(x)x^2-6;
x_lower=0;
x_upper=5;
x_mid=(x_lower+x_upper)/2;
while abs(myFunction(x_mid))>0.01
if (myFunction(x_mid)*myFunction(x_upper))<0
x_lower =x_mid;
else
x_upper = x_mid;
x_mid=(x_lower+x_upper)/2;
end
x_mid=(x_lower+x_upper)/2;
end
fprintf('the root is %g; x_mid)

4 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 4 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 4 日
Which line do you have issue?
Raban Nghidinwa
Raban Nghidinwa 2019 年 11 月 4 日
This is a numerical methods question which requires us to explain what the code does for 16 marks.
Star Strider
Star Strider 2019 年 11 月 4 日
So asking us to explain it to you is doing your homework for you, giving you an unfair advantage over your classmates who are doing this themselves, likely without any outside help.
Raban Nghidinwa
Raban Nghidinwa 2019 年 11 月 4 日
Mr Strider,I am new to MTLAB and we have limited resources but we are tested on an international scale so its struggle sometimes,but thank you so much.

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

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 4 日
編集済み: KALYAN ACHARJYA 2019 年 11 月 4 日

0 投票

Here myFunction evaluates the value based on x
myFunction=@(x)x^2-6;
Say
myFunction(2), here x=2 passes to function arguments, so the result will
ans =
-2
Then
Assigned
x_lower=0;
x_upper=5;
x_mid=(x_lower+x_upper)/2;
While loop continue till Absolute(myFunction(x_mid)) is greater than 0.01
{
Then replace the value of x_mid, x_lower, x_upper based on if condition
If true
x_lower change to x_mid
otherwise
x_upper=x_mid;
x_mid=(x_lower+x_upper)/2;
end if
Re-evaluate x_mid again based on present x_lower and x_upper value
Then print the x_mid value
Note: Text the code with some example, change the my function expression and see the results
Hope it helps!

1 件のコメント

Raban Nghidinwa
Raban Nghidinwa 2019 年 11 月 4 日
It helps,thank you.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by