Asked by Edward Lisa
on 18 May 2018

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?

%

Answer by KALYAN ACHARJYA
on 21 May 2018

Edited by KALYAN ACHARJYA
on 21 May 2018

Shown Graph the x ranges are different

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Adam (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401422-why-is-my-matlab-plot-wrong#comment_569371

## Edward Lisa (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401422-why-is-my-matlab-plot-wrong#comment_569387

## Stephen Cobeldick (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401422-why-is-my-matlab-plot-wrong#comment_569391

## Edward Lisa (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401422-why-is-my-matlab-plot-wrong#comment_569400

## Adam (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401422-why-is-my-matlab-plot-wrong#comment_570199

Sign in to comment.