Looking to write a code which utilises a formula to solve an equation

clc
clear all
close all
AF = @(x) x^3-4x^2-1; % function equation
xR(R/2) = 46/x + 5(x-3); % iterative formula
x3 = 1;
Very new to MATLAB and was wondering how I would go about creating an iteration to output solutions to the problem.

2 件のコメント

Mehmed Saad
Mehmed Saad 2020 年 4 月 13 日
編集済み: Mehmed Saad 2020 年 4 月 13 日
  1. what is xRk
  2. For how many iterations of R you want to run the formula
  3. what is the purpose of AF function in this code?
Sinclair Song
Sinclair Song 2020 年 4 月 13 日
1. My mistake, it's just meant to be xR, I've corrected it now
2. I'd say 100 to get possible solutions
3. AF is the function equation I'm trying to solve using the values generated from the iterative equation

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

回答 (1 件)

Mehmed Saad
Mehmed Saad 2020 年 4 月 13 日

0 投票

k=100;
xR = zeros(1,k);
xR(1) =2; % Initial Guess
for i=2:k
xR(i) = 1+ (1/xR(i-1)); % iterative formula
end
Now xR is an array which contains the value for 100 iteration where each iteration value depends on previous one

6 件のコメント

Sinclair Song
Sinclair Song 2020 年 4 月 13 日
Thank you for the response, I tried fitting your answer into the code but I don't seem to be receiving an output. Would an additional 'while' loop be required in order to fit in the desired error limit?
Mehmed Saad
Mehmed Saad 2020 年 4 月 13 日
err = 1;
x0 =2; % Initial Guess
while(err>0.05)
xR = 1+ (1/x0); % iterative formula
err = abs(xR-x0);
x0 = xR;
end
Sinclair Song
Sinclair Song 2020 年 4 月 13 日
Could I ask why the err was intiated with 1? The script isn't returning any errors nor any outputs.
Mehmed Saad
Mehmed Saad 2020 年 4 月 13 日
編集済み: Mehmed Saad 2020 年 4 月 13 日
i just initialized the err with 1, so that when while loop runs, it first check the condition, if err is not initilized then it will give error (undefined variable err) so we defined an initial value of variable err
write err in cmd
err
or just type err after the while loop end
You can see err in your workspace, it is less than 0.05.
If you want all the xR values, try indexing the array.
Sinclair Song
Sinclair Song 2020 年 4 月 13 日
編集済み: Sinclair Song 2020 年 4 月 13 日
Oh so it turns out the solution is too small to pick up, if I'm interpretting that correctly? How would I go about indexing an array? Thanks for all the guidance!
Mehmed Saad
Mehmed Saad 2020 年 4 月 13 日
Try using matlab help and google
in this way you can learn matlab faster

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

カテゴリ

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

タグ

タグが未入力です。

質問済み:

2020 年 4 月 13 日

編集済み:

2020 年 4 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by