How can I solve this ?

2 ビュー (過去 30 日間)
Mohammad Al Kotob
Mohammad Al Kotob 2021 年 5 月 19 日
コメント済み: Mohammad Al Kotob 2021 年 5 月 27 日
1. Create a function “secant method” that returns a variable “results”. These should later contain a vector of results from the iterations.
2. The method should transfer an x_0, x_1 value, the function and the number of iterations to get.
3. Initialize the vector with ones in the dimension 1 x (number of iterations +2).
4. Assign the x_0 and x_1 values to the first two digits of the vector.
5. Iterate over the result vector in a loop.
6. Now calculate the x_n value in each loop pass according to the Secant formula: x_n + 1 = x_n - (x_n - x_n_1) / (f (x_n) - f (x_n-1)) * f (x_n)
7.After the calculation, reassign the variables for the next run (keyword: x_n-1, x_n).
8. Add the current result to the result vector.
9. Call the newly created function with an anonymous function f = x ^ 2-2, 4 iterations and the starting values 1 and 2 (see lecture, exercise) and check the results.
10. Make sure that no errors or unnecessary output are displayed.
  2 件のコメント
David Hill
David Hill 2021 年 5 月 19 日
What have you done? Show us your code and ask a specific question.
Mohammad Al Kotob
Mohammad Al Kotob 2021 年 5 月 20 日
Hello , thats what i've done :
function [result]= secant method (x_0,x_1,f,k)
result = ones(1,(k+2));
result(1)= x_0; result(2)= x_1;
x_n=x_0;
x_n_1=x_1;
for k=1: length(result)
x_n1=x_n-(x_n-x_n_1)/(f(x_n)-f(x_n_1))*f(x_n);
x_n_1=x_n;
x_n=x_n1;
result(k)=x_n;
end
end

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

回答 (1 件)

Nagasai Bharat
Nagasai Bharat 2021 年 5 月 27 日
Hi,
Initially the function would show an error due to it's name(no space to be present between words). Correct way would be to use "secant_method".
The following documention would help you understaning the correct format while using function.
  1 件のコメント
Mohammad Al Kotob
Mohammad Al Kotob 2021 年 5 月 27 日
Thanks !

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by