A code to defined for the havlving for the trapezoidal rule

2 views (last 30 days)
Hi i like to ask is there a version of the codes which enables me to find the xnew in the halving for the xnew in the Romberg integration or
basically, if there is a line where a is the first point =0 and b is the end point=1; and h=b-a
I like to find is this xnew is to find for the xnew to be changing by half every loop
for example
for i=1:n
h=h/2; %Halfing the h for each new loop
xnew=a+i*h %To get the xnew every loop, but the problem i face here is how to get for the x new to be added into a vector form where the xnew values is also changing for each of the loop where the loop is filling with the xnew values in a vector form at the current loop.
another explaination is
for 1st iterations
h=h/2
xnew=a+h*1 which is also equals to xnew=0.5
for the 2nd Iterations
h=h/2 which is now =0.25 as the h was 0.5 from the previous loop
xnew =a+i*h, the problem i face here is that there should be two new values which are xnew =0.25 and 0.75
for the 3th iterations
h=h/2
xnew has to be equal to 0.125, 0.375, 0.625 and 0.875
and the loop goes on
to stop the loop let says as simple as just
err=0.1;
tol=0.01;
while err>tol
h=h/2 %we can use a for loop if it helps
xnew values
err=err-0.01;
end
This while loop is just keep the loop running as i want to find how many loops it required to reach for the err to be smaller than the tol, and what i also want is the xnew values at that iterations.
Thank you in advance

Answers (1)

Chidvi Modala
Chidvi Modala on 31 Mar 2021
You can make use of struct datatype. You may refer to the following code:
b=1;
a=0;
n = 100;
s = struct('xnew',0, 'h', b-a);
err = 0.1;
for i=1:n
if i==1
s.h(i) = (b-a)/2;
else
s.h(i)=s.h(i-1)/2;
end
s.xnew(1:i) = a+i.*(s.h(1:i));
err = err-0.01;
if err<0.01
index = i;
break;
end
end

Categories

Find more on Programming 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!