Add another column to the right end of this array

1 次查看(过去 30 天)
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

回答(1 个)

James Tursa
James Tursa 2023-3-29
your_matrix(:,end+1) = 1.5;
  4 个评论
Torsten
Torsten 2023-3-29
编辑:Torsten 2023-3-29
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
my_results = 12×4
1.0000 0.5000 66.6667 1.5000 2.0000 1.2500 16.6667 1.5000 3.0000 1.3889 7.4074 1.5000 4.0000 1.4375 4.1667 1.5000 5.0000 1.4600 2.6667 1.5000 10.0000 1.4900 0.6667 1.5000 20.0000 1.4975 0.1667 1.5000 30.0000 1.4989 0.0741 1.5000 40.0000 1.4994 0.0417 1.5000 50.0000 1.4996 0.0267 1.5000

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by