Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

unable to plot after calling a function...

1 Ansicht (letzte 30 Tage)
derek lukasik
derek lukasik am 28 Sep. 2017
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
This is what I am supposed to do: "Create a plot of the function f(x) = xe^x over the interval x ∈ [−1, 1]. On the same graph,plot your function L(x) for values of x ∈ [f(−1), f(1)]. Add the reference line y = x, and lines indicating the x and y axis. Add labels, a title and a legend to your plot. See Figure 2 for an example of what your plot should look like."
Below is my code where I make a function call then try to plot (y).I can solve for any value of c but can not plot. What am I doing wrong?
if true
% code
%%Problem 3 : Inverse of y=x*exp(x)
function prob3()
close all
% Your work goes here
c=4; %input c value
y = L(c)
x =-1:1:100;
plot(x,y)
end
This function is what you need to write for Problem 3
function y = L(c)
The function y = L(x) is the inverse of f(x) = x*exp(x) Your goal with this problem is to solve "c = x*exp(x)" for x, given the input argument c.
for j=1:length(c)
f=@(x)x.*exp(x)-c(j);
ff=@(x)(1+x).*exp(x); %given function and its derivative, both vectorized
i=0; %counts the number of iterations
xn=c(j);
tol=(-1/exp(1));
while (abs(f(xn))>tol) %if the error is not within tolerance level'
i=i+1; %count number of iterations'
if(i>1000) %if number of iterations is more than 1000 to prevent infinite loops
display('Method terminated without finding root to sufficient accuracy') %terminate the method
break;
else
xn=xn-f(xn)/ff(xn); %newton's method iteration scheme
end
end
sol(j)=xn;
end
y = xn;
% At the very end, you need to be sure you assign "y" to something
% y = ....
end
end

Antworten (0)

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by