I need help plotting this, i Cant figure out what is wrong

fun = @(x) x.^3 - 10*x.^2 + 16*x +80;
a = 0;
b = 6;
for n = [2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 18, 20]
I = A9P1TRAPIdrees(fun,a,b,n);
err = abs((372-I)/372)*100;
fprintf('n = %d value=%f error= %f\n', n, I, err);
end
subplot(3,1,1); %f(x) vs. x
fplot(fun,[0 6])
title({['Ragheed Idrees ',datestr(now)];'';'f(x) vs x)'})
xlabel('x')
ylabel('f(x)')
subplot(3,1,2); %Integral vs number of sugments
plot(n,I)
title('Integral vs number of sugments')
xlabel('number of segments')
ylabel('Integral')
subplot(3,1,3); %Percent error vs number of sugments
loglog(n,err)
title('Percent error vs number of sugments')
xlabel('number of segments')
ylabel('Percent Error')
-----------------------------------------------------------------------------A9P1TRAPIdrees-------------------------------------------------------------------------------------------------------------
function I = A9P1TRAPIdrees(fun,a,b,n)
%this function uses the trapezoidal rule to integrate a function with given
%boundaries and number of segments
%defining the output
h = (b-a)/n;
s=0.5 * (fun(a)+fun(b));
I=0;
for jj = 1 : n-1
s = s +fun(a + jj*h);
end
I = h *s;
it gives me a warning "Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size
and shape as the input arguments. "
and the last 2 subplots wont plot!

 Accepted Answer

fun = @(x) x^3 - 10*x.^2 + 16*x +80;
should be
fun = @(x) x.^3 - 10*x.^2 + 16*x +80;

5 Comments

oh thank you, i fixed it, somehow it didnt change anything, my calculations still come out correct:
n = 2 value=363.000000 error= 2.419355
n = 3 value=368.000000 error= 1.075269
n = 4 value=369.750000 error= 0.604839
n = 5 value=370.560000 error= 0.387097
n = 6 value=371.000000 error= 0.268817
n = 8 value=371.437500 error= 0.151210
n = 10 value=371.640000 error= 0.096774
n = 12 value=371.750000 error= 0.067204
n = 14 value=371.816327 error= 0.049375
n = 16 value=371.859375 error= 0.037802
n = 18 value=371.888889 error= 0.029869
n = 20 value=371.910000 error= 0.024194
loglog(n,err)
That line is after the end of the for n loop. After the for loop, n will be left at the last value it was set to inside the loop, so the scalar 20 in this case. It will still be the scalar when it reaches the loglog(n,err) call.
err = abs((372-I)/372)*100;
That is within your loop. Your err variable is going to be a scalar too: you are overwriting it every iteration.
nvals = [2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 18, 20];
numn = length(nvals);
err = zeros(1, numn);
for nidx = 1 : numn
n = nvals(nidx);
...
err(nidx) = abs((372-I)/372)*100;
end
loglog(nvals, err)
That helped alot! again thank you, i tried to do the same for I but i still cant get the second plot to run
nvals = [2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 18, 20];
numn = length(nvals);
err = zeros(1, numn);
I = zeros(1, numn);
for nidx = 1 : numn
n = nvals(nidx);
I(nidx) = A9P1TRAPIdrees(fun,a,b,n);
err(nidx) = abs((372-I(nidx))/372)*100;
fprintf('n = %d value=%f error= %f\n', n, I(nidx), err(nidx));
end
subplot(3,1,1); %f(x) vs. x
fplot(fun,[0 6])
title({['Ragheed Idrees ',datestr(now)];'';'f(x) vs x)'})
xlabel('x')
ylabel('f(x)')
subplot(3,1,2); %Integral vs number of sugments
plot(nvals, I)
title('Integral vs number of sugments')
xlabel('number of segments')
ylabel('Integral')
subplot(3,1,3); %Percent error vs number of sugments
loglog(nvals, err)
title('Percent error vs number of sugments')
xlabel('number of segments')
ylabel('Percent Error')
-----------------------------------------------------------------------------A9P1TRAPIdrees-------------------------------------------------------------------------------------------------------------
that checked everything, thangs again!

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!