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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1339789/image.jpeg)
I would like to obtain this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1339794/image.jpeg)
thanks for your help
0 个评论
回答(1 个)
James Tursa
2023-3-29
your_matrix(:,end+1) = 1.5;
4 个评论
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
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!