Help me plot my first function, please

5 views (last 30 days)
Hello. I'm a complete beginner when it comes to MATLAB and couldn't find a question that helped me. (Probably because I don't know the right terms to search). Sorry.
Why can't I plot this function? It worked on a smaller scale in a test file i made.
I get error: "Undefined function or variable 'r'." But I defined it in the function.
tx = 40 * 10^-10;
ex = 3.45*10^-13;
on = 180;
Vn = 0.25;
WLn = 0.12/1.4;
B = (on * (ex/tx)*WLn)
fplot(r)
function r = In(Vsn)
r = B*(1.2 - Vn - (Vsn/2))*Vsn
end

Accepted Answer

Walter Roberson
Walter Roberson on 13 Feb 2017
You could use:
tx = 40 * 10^-10;
ex = 3.45*10^-13;
on = 180;
Vn = 0.25;
WLn = 0.12/1.4;
B = (on * (ex/tx)*WLn);
r = @(Vsn) B*(1.2 - Vn - (Vsn/2)).*Vsn;
fplot(r)
The above can be used as a script file.
Any variable that you define in a function only exists while the function is executing.
You were not executing the function named In at all.
Also, your function used values that were defined in the script and not passed into the function.
You could also have used
function my_first_plot
tx = 40 * 10^-10;
ex = 3.45*10^-13;
on = 180;
Vn = 0.25;
WLn = 0.12/1.4;
fplot(@In)
function r = In(Vsn)
r = B*(1.2 - Vn - (Vsn/2))*Vsn
end
end
This is a more advanced program that shares variables with a nested function.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!