Hi! I just started my Matlab course and i have run into a little problem which i hope you can help me with. I am suppose to write a function which integrates a function e^(-t^2) from 0 to some x which is a variable in the function. I did that like this:
function y = simpson_method(x)
y1 = exp(-0.^2);
y2 = exp(-(x).^2);
c = 0;
d = 0;
incr = 0.1;
for i = 1 : (x - 0)/incr - 0.5
y = exp(-(0 + i*incr).^2);
if i/2 == floor(i/2)
d = d + y;
continue
else
c = c + y;
continue
end
end
y = incr/3 * (y1 + 4*c + 2*d + y2);
The function integrates correctly and gives me a desired output for every x. But the problem is when i try to plot it:
x = 0:0.1:15;
y = simpson_method(x);
plot(x,y)
It gives me the wrong graph which i find weird because all the outputs are correct. I get this which is nonsense. Whats happening?
%

댓글 수: 5

Adam
Adam 2018년 5월 18일
What is the right graph?
And what are the values of y that you get out?
Edward Lisa
Edward Lisa 2018년 5월 18일
This is the right plot and my function outputs y as very close to 0 when x=0 and reaches 0.884.. when x goes larger which is exactly the plot of the function when integrated on https://www.integral-calculator.com/ with input e^(-x^2) from 0 to your chosen upper limit.
Stephen23
Stephen23 2018년 5월 18일
@Edward Lisa: can you please confirm the ambiguous syntax:
  1. e^((-t)^2)
  2. e^(-(t^2))
Edward Lisa
Edward Lisa 2018년 5월 18일
Its e^(-(t^2))
Adam
Adam 2018년 5월 21일
If your y values are correct I can see no reason at all why plot would not give the correct answer so your y values cannot be correct if you are literally just calling plot.

댓글을 달려면 로그인하십시오.

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 5월 21일
편집: KALYAN ACHARJYA 2018년 5월 21일

0 개 추천

Shown Graph the x ranges are different

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2018년 5월 18일

편집:

2018년 5월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by