function f(x)=xe^x

Asked by Deanna Jaramillo

Deanna Jaramillo (view profile)

on 24 Aug 2019
Latest activity Commented on by Steven Lord

Steven Lord (view profile)

on 27 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.

Steven Lord

Steven Lord (view profile)

on 24 Aug 2019
This sounds like a homework assignment. If it is, show us what you've done to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to offer some guidance.
Deanna Jaramillo

Deanna Jaramillo (view profile)

on 27 Aug 2019
yes it was a homework problem. I do not understand much code and I am wanting to learn. The instructor showed us some 'for' loops and 'while' loops and a function program and I am not sure when to use what. Here is the question for the first part.
a) Considering the function
f(x) = xe^x (1)
(a) Write a function my_fun.m which returns the value of (1) given an input x. This function should be a function of x only.
I came up with:
function y=my_fun(x)
y=x*exp(x);
end
Then the next part:
b) Write a function my_mean.m which takes four arguments: 1) a function (my_fun.m), 2) a number a, 3) another number b satisfying a ≤ b, and 4) a positive integer N. This function should return an approximate value for: (2)
Where f(x) represents (1). The approximate value of (2) should be calculated via: where:
h = (b − a)/ N , xj = a + (j − 1)h
HW #1 For example, a call to the function would resemble:
my_mean(@my_fun, 0, 5, 100)
(c) Use my_mean.m and my_fun.m to compute an approximation to In this case the exact solution for (4) can be computed as:
1/ e
Plot (using semilogy) the absolute error between M and your approximations versus N for:
N(n) = 2n , 1 ≤ n ≤ 13
So, for the codes, I have:
function y= my_mean(my_fun,a,b,N)
h=(b-a)/N;
x=linspace(a,b,N);
y=0;
for j=1:length(x)
y = (1/N)*(a+((j-1)*h));
end
end
and the final for the output plot:
close all
clear
clc
format longg
n=1:13;
N=2.^n;
M=0;
for j=1:length(N)
M(j)=abs(my_mean(@my_fun,-1,1,N(j)));
end
semilogy(N,M,'-ob');
xlabel('N', 'Fontsize', 10);
ylabel('M', 'Fontsize', 10);
title('Absolute Error', 'Fontsize', 15)
I got the plot to show up but the y-axis goes from 10^-4 to 10^-1, when the instructor's goes from 10^-4 to 10^0.
How do I know what to use when?
-Deanna
Steven Lord

Steven Lord (view profile)

on 27 Aug 2019
James already discussed your parts b and c, so I just want to offer a little extra information. This is slightly more advanced than you may have learned so far, but it's something that can come in very useful when working with MATLAB.
For part a your function will work as long as the x input to my_fun is a scalar (just one number.) You could modify it ever so slightly to allow it to work on any sized array (a scalar, a vector with ten elements, a 5-by-5 matrix, a 2-by-3-by-4-by-5-by-6 array, etc.) by using the array multiplication operator .* instead of the matrix multiplication operator *. [For scalars they behave the same; for non-scalars they don't.] See the Array vs. Matrix Operations documentation page for more information.

Release

R2019a

Answer by Star Strider

Star Strider (view profile)

on 24 Aug 2019

Answer by James Tursa

James Tursa (view profile)

on 27 Aug 2019
Edited by James Tursa

James Tursa (view profile)

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.