Asked by Deanna Jaramillo
on 24 Aug 2019

Write a function my_fun.m which returns the value of f(x)=xe^x given an input x. This

function should be a function of x only.

Answer by James Tursa
on 27 Aug 2019

Edited by James Tursa
on 27 Aug 2019

Take a look at this loop from your code:

for j=1:length(x)

y = (1/N)*(a+((j-1)*h)); % <-- This replaces y at each step ... it doesn't sum anything up!

end

And compare it to the summation (3) in the instructions. Two problems: You are not calling your function, and you are not summing anything up. You should be summing up f(xj) values (N of them) and then dividing that sum by N according to the (3) formula. So the loop should be something like this instead:

for j=1:N % <-- Use the limits in the (3) formula

y = y + my_fun(a+(j-1)*h); % <-- Sums up individual calculations into y

end

y = y/N;

You don't need (or want) that x-linspace(etc) call.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Steven Lord (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/477462-function-f-x-xe-x#comment_738675

## Deanna Jaramillo (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/477462-function-f-x-xe-x#comment_739428

## Steven Lord (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/477462-function-f-x-xe-x#comment_739529

Sign in to comment.