Looking to write a code which utilises a formula to solve an equation
Ältere Kommentare anzeigen
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 Kommentare
Mehmed Saad
am 13 Apr. 2020
Bearbeitet: Mehmed Saad
am 13 Apr. 2020
- what is xRk
- For how many iterations of R you want to run the formula
- what is the purpose of AF function in this code?
Sinclair Song
am 13 Apr. 2020
Antworten (1)
Mehmed Saad
am 13 Apr. 2020
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 Kommentare
Sinclair Song
am 13 Apr. 2020
Mehmed Saad
am 13 Apr. 2020
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
am 13 Apr. 2020
Mehmed Saad
am 13 Apr. 2020
Bearbeitet: Mehmed Saad
am 13 Apr. 2020
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
am 13 Apr. 2020
Bearbeitet: Sinclair Song
am 13 Apr. 2020
Mehmed Saad
am 13 Apr. 2020
Try using matlab help and google
in this way you can learn matlab faster
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!