Add another column to the right end of this array

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)

your_matrix(:,end+1) = 1.5;

4 Comments

Thanks for your response.
Where should I put it? I have tried but I dont know where to insert that command...
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
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
thank you very much for your help.....i appreciate it

Sign in to comment.

Products

Asked:

on 29 Mar 2023

Commented:

on 29 Mar 2023

Community Treasure Hunt

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

Start Hunting!