Help me plot my first function, please

5 vues (au cours des 30 derniers jours)
Magnarok
Magnarok le 13 Fév 2017
Commenté : Magnarok le 13 Fév 2017
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

Réponse acceptée

Walter Roberson
Walter Roberson le 13 Fév 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.
  1 commentaire
Magnarok
Magnarok le 13 Fév 2017
Is Vsn now the x-axis?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by