Using a variable as both output and input and writing a condition in a function

Hei , I have this function
function [lambda, lambdap,phip] = Palaeomagfun(I ,D,lambda,lambdax,phix )
lambda=atand(tand(I)/2) %in degree
lambdap=asind(sind(lambdax)*sind(lambda)+cosd(lambdax)*cosd(lambda)*cosd(D))
if sind(lambda)>=sind(lambdap)*sind(lambdax)
phip=phix+asind(cosd(lambda)*sin(D)/cosd(lambdap))
elseif sind(lambda)<sind(lambdap)*sind(lambdax)
phip=phix+asind(cosd(lambda)*sind(D)/cosd(lambdap))-180
end
and this script
Problem 2:
I=30
lambdax=47
phix=20
D=80
[lambda, lambdap,phip] = Palaeomagfun(I ,D,lambda,lambdax,phix )
*first,Is it possible to use lambda as an output and input at the same time or i need two function or something else?
second: is my if coding correct in the function?*

2 comentarios

In general, yes, it is possible to use the same variable as input and output, both in the function definition and in the function call, the two not being the same thing.
However, in your case passing in lamda is redundant anyway since you recalculate it straight away.
that is because i need first to calculate lambda which is a function of I and then i need to use this lambda again in the other calculation maybe i need to function or its possible to have a function inside function?

Iniciar sesión para comentar.

 Respuesta aceptada

KL
KL el 21 de Nov. de 2017
Editada: KL el 21 de Nov. de 2017
When you write
function [lamda, lambdap,phip] = Palaeomagfun(I ,lamda,lamdax,phix )
lamda=atand(tand(I)/2) %in degree
the input lamda you pass would be overwritten because of your first line inside the function.
How about using two variables, say lamda_in and lamda_out
function [lamda_out, lambdap,phip] = Palaeomagfun(I ,lamda_in,lamdax,phix )

8 comentarios

i did that and change lamda in the equation to lambda_in but then it says'Undefined function or variable 'lamda_in''
KL
KL el 21 de Nov. de 2017
Editada: KL el 21 de Nov. de 2017
oh oh.. typo ( lambda_in vs lamda_in). You must have simply copied my line. I was just trying to give you an example. I fixed my typo now.
F.O
F.O el 21 de Nov. de 2017
Editada: F.O el 21 de Nov. de 2017
Actually i have messed out because all of them should have letter b i wrote b in all lamda
but it doesint seem working
but it doesint seem working
You have to provide more information! We cannot guess what do you are trying to do there.
When you write,
[lambda, lambdap,phip] = Palaeomagfun(I ,D,lambda,lambdax,phix )
all your variables you pass into your functions must be defined previously. As long as you don't do that you will get "undefined function or variable error".
Initialize that variable,try again and make sure you know what you are doing inside your function.
I have defined my variable with equtions and have values of the output in the script but the problem is with one variable which it should be a calculated as output and then we can use it to calculate the other two outputs
Unfortunately it's still not very clear but if you want to calculate lambda based on I, write a function,
function lambda = findLambda(I)
your equation for lambda here
end
and then use this function return directly on your other function call,
[lambda, lambdap,phip] = Palaeomagfun(I ,D,findLambda(I),lambdax,phix )
I still do not understand your intention. But as Adam said, it is possible to have the same variable as input and output. But if reassign the input variable before using it's old value, it doesn't make any sense.
F.O
F.O el 21 de Nov. de 2017
Editada: F.O el 21 de Nov. de 2017
My intention is what is the best way (using function) to calculate lambda which is function of I and then calculate lambdap,phip but these two need lambda to get calculated

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

F.O
el 21 de Nov. de 2017

Editada:

F.O
el 21 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by