using for loop plotting problem help me

function homework
f= @(x) 3*x^5+7*sind(x)^2;
fd= @(x) 15*x^4+7*sind(2*x)^2+3*x^5+7*sind(x)^2;
a=-1;
b=3;
n=5;
e=1.1*10^3;
s=(fd(a)+fd(b));
for i=1:1:n-1
h=(b-a)/i;
s=s+2*fd(i);
I=(h/2).*s
error=I-e
end
end
the problem is ı have values but try everything for plotting but see nothing even there is no line or spline .

3 comentarios

Cris LaPierre
Cris LaPierre el 12 de En. de 2021
Why do you expect to see a plot? You haven't included any plotting commands. See Ch 9 of MATLAB Onramp for help with plotting.
ataberk kaplica
ataberk kaplica el 12 de En. de 2021
i did all plot type , but get nothing
Rik
Rik el 12 de En. de 2021
This time I edited your question for you (also removing most blank lines). Next time, please use the tools explained on this page to make your question more readable.
I don't understand your comment. Your function does not contain any plotting functions, and at the end of a function all variables that aren't output variables will be wiped.
Also, you overwrite the contents of you variables every iteration of your loop. You may also want to look at the Onramp part concerning loops and indexing.

Iniciar sesión para comentar.

 Respuesta aceptada

Mathieu NOE
Mathieu NOE el 12 de En. de 2021
hello
I assumed that you wanted to plot I and error , but you forgot to index thos variables
see below :
f= @(x) 3*x^5+7*sind(x)^2;
fd= @(x) 15*x^4+7*sind(2*x)^2+3*x^5+7*sind(x)^2;
a=-1;
b=3;
n=5;
e=1.1*10^3;
s=(fd(a)+fd(b));
for i=1:1:n-1
h=(b-a)/i;
s=s+2*fd(i);
I(i)=(h/2).*s;
error(i)=I(i)-e;
end
figure(1),plot(1:n-1,I,'b')
figure(2),plot(1:n-1,error,'r')

6 comentarios

ataberk kaplica
ataberk kaplica el 12 de En. de 2021
yes thank you but more like ı want to error vs h graph how can ı do that
ataberk kaplica
ataberk kaplica el 12 de En. de 2021
for 1:n-1 range
ok
so also h needs to be indexed in your loop
final code is :
f= @(x) 3*x^5+7*sind(x)^2;
fd= @(x) 15*x^4+7*sind(2*x)^2+3*x^5+7*sind(x)^2;
a=-1;
b=3;
n=5;
e=1.1*10^3;
s=(fd(a)+fd(b));
for i=1:1:n-1
h(i)=(b-a)/i;
s=s+2*fd(i);
I(i)=(h(i)/2).*s;
error(i)=I(i)-e;
end
figure(1),plot(h,error,'r')
ataberk kaplica
ataberk kaplica el 12 de En. de 2021
thank you its work
Mathieu NOE
Mathieu NOE el 12 de En. de 2021
you're welcome !
Rik
Rik el 12 de En. de 2021
Just a note: you should avoid using function names as variable names. It is very common to want to use the error function, so shadowing it is a bad habit (similar to max, mean, and sum).

Iniciar sesión para comentar.

Más respuestas (1)

David Hill
David Hill el 12 de En. de 2021
What are you trying to plot?
f= @(x) 3*x.^5+7*sin(x).^2;%I doubt you want sind
fd= @(x) 15*x.^4+7*sin(2*x).^2+3*x.^5+7*sin(x).^2;
x=-1:.01:3;
plot(x,f(x),x,fd(x));

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 12 de En. de 2021

Comentada:

Rik
el 12 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by