How can I the show the solution/results in table form?
12 views (last 30 days)
Show older comments
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)
0 Comments
Answers (1)
Benjamin Kraus
on 2 Aug 2022
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)
fprintf('Integrated Value is %0.4f Sq. Unit\n', I)
2 Comments
Benjamin Kraus
on 2 Aug 2022
I don't know the specifics of the Simpsons 1/3 Rule, so I'll leave the details of the algorithm up to you.
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!