Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Storing found values in array
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
Fangjun Jiang
2020 年 3 月 30 日
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
0 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!