How can I the show the solution/results in table form?

13 ビュー (過去 30 日間)
Christoppe
Christoppe 2022 年 8 月 2 日
コメント済み: Benjamin Kraus 2022 年 8 月 2 日
Hi! I'm doing a Simpsons 1/3 rule Numerical Methods, How can I show the results each solution in table form before the answer?
Example:
x f(x) (Odd or Even) f(x)*(Odd or Even)
-2.00000 0.05399 1.00000 0.05399
-1.55000 0.12001 4.00000 0.48004
-1.10000 0.21785 2.00000 0.43570
-0.65000 0.32297 4.00000 1.29188
-0.20000 0.39104 2.00000 0.78208
0.25000 0.38667 4.00000 1.54668
0.70000 0.31225 2.00000 0.62450
1.15000 0.20594 4.00000 0.82376
1.60000 0.11092 1.00000 0.11092
summation of [f(xi)* (odd or even)] 6.14955
Answer = Sq. Unit
Here;s my code:
eqn= 0.2+(25*x)-(200*x^2)+(675*x^3)-(900*x^4)+(400*x^5)
a= 0
b= 0.8
n= 8
clear
clc
format short
P=input ('Enter the Equation: ','s');
f=inline(P)
a=input ('Enter the Lower Value Limit: ')
b=input ('Enter the Upper Value Limit: ')
disp('Number of Segments "n" should be Divisible by 2!')
n=input ('Enter the Number of Segments: ')
h=round((b-a)/n,4)
odd=0;
even=0;
for i=1:2:n-1
x=round(a+i*h,4);
odd=round(odd+f(x),4);
end
for i=2:2:n-2
x=round(a+i*h,4);
even=round(even+f(x),4);
end
interc(i,:)=[x,f(x)]
I=round((h/3)*(f(a)+(4*odd)+(2*even)+f(b)),4);
fprintf('Integrated Value is %0.4f Sq. Unit', I)

回答 (1 件)

Benjamin Kraus
Benjamin Kraus 2022 年 8 月 2 日
There is still some polish you probably need to make, but here is one approach to displaying your results in a table:
a = 0;
b = 0.8;
n = 8;
f = @(x) 0.2+(25*x)-(200*x^2)+(675*x^3)-(900*x^4)+(400*x^5);
h = round((b-a)/n,4);
odd = 0;
even = 0;
oddOrEven = NaN(n-1,1);
x = NaN(n-1,1);
interc = NaN(n-1,1);
for i = 1:2:n-1
x(i) = round(a+i*h,4);
interc(i) = f(x(i));
odd = round(odd+interc(i),4);
oddOrEven(i) = odd;
end
for i = 2:2:n-2
x(i) = round(a+i*h,4);
interc(i) = f(x(i));
even = round(even+interc(i),4);
oddOrEven(i) = even;
end
I = round((h/3)*(f(a)+(4*odd)+(2*even)+f(b)),4);
results = table(x, interc, oddOrEven, interc.*oddOrEven, ...
'VariableNames',{'x','f(x)','(Odd or Even)','f(x)*(Odd or Even)'});
disp(results)
x f(x) (Odd or Even) f(x)*(Odd or Even) ___ _____ _____________ __________________ 0.1 1.289 1.289 1.6615 0.2 1.288 1.288 1.6589 0.3 1.607 2.896 4.6539 0.4 2.456 3.744 9.1953 0.5 3.325 6.221 20.685 0.6 3.464 7.208 24.969 0.7 2.363 8.584 20.284
fprintf('Integrated Value is %0.4f Sq. Unit\n', I)
Integrated Value is 1.6395 Sq. Unit
  2 件のコメント
Christoppe
Christoppe 2022 年 8 月 2 日
編集済み: Christoppe 2022 年 8 月 2 日
Hello Sir! I appreciate your prompt response. May I ask is it possible to change the value of Odd or Even. Like example First and Last Rows will be multiplied by 1, All Odd rows will be multiplied by 4, and All Even rows will be multiplied by 2...
Benjamin Kraus
Benjamin Kraus 2022 年 8 月 2 日
I don't know the specifics of the Simpsons 1/3 Rule, so I'll leave the details of the algorithm up to you.

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

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by