Info

This question is closed. Reopen it to edit or answer.

Storing found values in array

1 view (last 30 days)
Bradley Johnson
Bradley Johnson on 30 Mar 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello I have a code set to do a bisection method. I need to store the values of a, b, c, x,and y for each iteration of the code so i can make a table and plot them. How would i go about doing that?
clear; clc;
f=@(x)2 * x^4 +3 * x^3 - 11 * x^2 - 9 * x + 15 ;
a=input('\n Enter left point of interval ');
b=input('\n Enter right point of interval ');
toll= 1e-4;
err=abs(b-a);
c = ( a + b )/2;
if f(a) *f(b) > 0
disp 'Enter valid interval!!!');
else
c = ( a + b) /2;
toll= 1e-4;
err=abs(b-a);
while err > toll
if abs(f(c)) < err
else
c = ( a + b )/2;
x = abs( b - c);
y = abs(f(c));
if ( f(a) * f(c)) <= 0
b = c;
else
if ( f(b) * f(c)) <= 0
a = c;
end
end
end
c = ( a + b) /2;
err = abs(b-a);
end
fprintf( 'The Root is: %4.4f\n', c)
end

Answers (1)

Fangjun Jiang
Fangjun Jiang on 30 Mar 2020
I had this example code for answering a similar question. I think you can utilize it as an example for storing while-loop results.
x=100;y=200;
k=0;
result=ones(100,2);
while x>1e-3 && y>1e-4
k=k+1
[x,y]=D23(x,y);
result(k,:)=[x,y];
end
result(k+1:end,:)=[]
function [x,y]=D23(x,y)
x=x/2;
y=y/3;
end

This question is closed.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!