Add another column to the right end of this array
Show older comments
This a code within the book of Cengels' Differential Equations. I would like to add a column with the value exact (1.5) at the right side at the end of the array.
lower=0;upper=1;
m=1;
exact=1.5;
f=@(x)7*x-6*x^2;
for nstrip=[1,2,3,4,5,10:10:50,100,200]
h=(upper-lower)/nstrip;
x=lower;value=0;
for n=1:nstrip
value=value+h*(f(x)+f(x+h))/2;
x=x+h;
error(m,:)=[nstrip,value,100*abs(value-exact)/exact];
end
m=m+1;
end
error
This is what I obtain:

I would like to obtain this

thanks for your help
Answers (1)
James Tursa
on 29 Mar 2023
your_matrix(:,end+1) = 1.5;
4 Comments
Jose Ceballos
on 29 Mar 2023
James Tursa
on 29 Mar 2023
After you build the matrix, put this line afterwards using the actual name of your matrix instead of 'your_matrix' as I have listed.
lower=0;upper=1;
m=1;
exact=1.5;
f=@(x)7*x-6*x^2;
Nstrip=[1,2,3,4,5,10:10:50,100,200];
my_results = zeros(numel(Nstrip),4);
for nstrip=Nstrip
h=(upper-lower)/nstrip;
x=lower;value=0;
for n=1:nstrip
value=value+h*(f(x)+f(x+h))/2;
x=x+h;
end
my_results(m,:) = [nstrip,value,100*abs((value-exact)/exact),exact];
m=m+1;
end
my_results
Jose Ceballos
on 29 Mar 2023
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!