how to run operation row by row. Ex row(n) divide row(n+1)

4 ビュー (過去 30 日間)
Noriham B
Noriham B 2023 年 3 月 2 日
コメント済み: DUY Nguyen 2023 年 3 月 2 日
clc
clear
syms f(x)
f(x) = exp(-((x).^2));
a =0; % lower limit
b = 1; %upper limit
for j=1:6
n = 2.^j;
h = (b-a)/n;
kamir = vpa(int(f,x,a,b));
s = 0.5*(f(a)+f(b));
for i=1:n-1
s = s+f(a+i*h);
end
I =vpa(h*s);
fprintf(' \t %6d \t %f.4 \n ',n,I)
end
Above is my code. after run, we will get 2 column...
for the second column...what I want to do is...I want to compute row(n)/row(n+1)
Hopefully dear all professor/Dr/expert can help me with this problem..thank you in advance

採用された回答

DUY Nguyen
DUY Nguyen 2023 年 3 月 2 日
Hi, in this modified code, I added a prev_I variable to store the previous value of I. After computing I, we compute the ratio as I / prev_I and print it in the third column!
clear
syms f(x)
prev_I=1;
f(x) = exp(-((x).^2));
a =0; % lower limit
b = 1; %upper limit
fprintf('n\t\tIntegral\tRatio\n');
n Integral Ratio
for j= 1:6
n = 2.^j;
h = (b-a)/n;
kamir = vpa(int(f,x,a,b));
s = 0.5*(f(a)+f(b));
for i=1:n-1
s = s+f(a+i*h);
end
I =vpa(h*s);
if i==1
ratio =1;
else
ratio = I / prev_I;
end
fprintf('%d\t\t%.4f\t\t%.4f\n', n, I, ratio)
prev_I = I;
end
2 0.7314 1.0000 4 0.7430 1.0159 8 0.7459 1.0039 16 0.7466 1.0010 32 0.7468 1.0002 64 0.7468 1.0001
  3 件のコメント
Noriham B
Noriham B 2023 年 3 月 2 日
its working actually..juz that
should be
else
ratio = prev_I/I ;
the rest is ok...tq so much sir for helping me...
now its working..may God bless u
DUY Nguyen
DUY Nguyen 2023 年 3 月 2 日
Oh, yes! I see.
No problem :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by