Add another column to the right end of this array
1 view (last 30 days)
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
0 Comments
Answers (1)
James Tursa
on 29 Mar 2023
your_matrix(:,end+1) = 1.5;
4 Comments
Torsten
on 29 Mar 2023
Edited: Torsten
on 29 Mar 2023
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
See Also
Categories
Find more on Linear Algebra 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!