How do I set some variables of a function to a constant and plot the others?

I defined a function Theta of two variables. What is the simplest way to set one variable to a constant and make a 2D plot using the other variable? For instance, I would like to set Delta = 1 and plot Theta(E,1) over E -3:.001:3?
function Theta = Theta(E,Delta)
if abs(E)<Delta
Theta = pi/2 + 1i*atanh(E/Delta);
elseif abs(E)>Delta
Theta = 1i*atanh(E/Delta);
end
end

 Risposta accettata

This is a possibility
E = -3:.001:3;
plot(E,Theta(E,1))
Warning: Imaginary parts of complex X and/or Y arguments ignored.
function out = Theta(E,Delta)
out = zeros(size(E));
out(abs(E)<=Delta) = pi/2+1i*atanh(E(abs(E)<=Delta)/Delta);
out(abs(E)>Delta) = 1i*atanh(E(abs(E)>Delta)/Delta);
end

4 Commenti

Thank you David! Would you mind explaining what you are doing in the line "out = zeros(size(E));"? Also, did you change Theta on the lhs of the first line and below to "out" just to avoid confusion with Theta(E,Delta) or did you have some other purpose?
@Zachary NEVER name your function with the same name as a variable in that function, or a variable returned from the function, etc. If you do, then expect that MATLAB will be terribly confused. It will want to know which Theta you are asking it to use, thus Theta as a function or Theta as the variable? So perhaps it might guess that you want Theta to be a recursive function. Wanting a computer to guess what you are thinking is always a bad idea.
Using the same names there is just asking for massive problems in the end. Even if only for you, when you ty to debug or even read your own code.
And remember that MATLAB does not charge you more for the use of an extra variable name. Names are FREE!
Sorry for the slow response, I don't have anything to add to John's extensive explanation.
Just mind that the function as it is now works only in case you want to keep delta constant and plot theta vs E.
More robust codes can be implemented depending on your needs.
Lastly, if you think the answer is comprehensive enough and helped, please consider accepting it.
Cheers

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graphics Performance in Centro assistenza e File Exchange

Prodotti

Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by