# Thread: Matlab not returning variable as intended

1. ## Matlab not returning variable as intended

I know this is a super simple problem that has been discussed before but I cannot find the answer for the life of me.
All I want is for this program to output the variable as Y and have it correctly place it in the workspace as Y.

Code:
function [y] = stress(Ri,Ro,R,Delta,E)
%stress = stress(Ri,Ro,R,Delta,E)

y = Delta*E/R*1/((R^2+Ri^2)/(R^2-Ri^2)+(R^2+Ro^2)/(R^2-Ro^2));

end
What am I doing wrong?

2. Originally Posted by canyon289
I know this is a super simple problem that has been discussed before but I cannot find the answer for the life of me.
All I want is for this program to output the variable as Y and have it correctly place it in the workspace as Y.

Code:
function [y] = stress(Ri,Ro,R,Delta,E)
%stress = stress(Ri,Ro,R,Delta,E)

y = Delta*E/R*1/((R^2+Ri^2)/(R^2-Ri^2)+(R^2+Ro^2)/(R^2-Ro^2));

end
What am I doing wrong?
Where is this function? It should be a function in a file called stress.m (and on the matlab path) or in the script file (I think). It cannot be a sub-function in another .m file.

CB

3. It's a function file called stress.m, It's not a sub function of anything

4. Originally Posted by canyon289
It's a function file called stress.m, It's not a sub function of anything
Exactly what are the symptoms of it not doing what you expect.

CB

5. Referring to the attached screenshot

The output of the function is saved as ANS when I want it saved as Y.
See how I inputted a variable for 1, ran my program and it didn't change the value of Y but rather added another scalar named ANS

Thanks for you help

6. Originally Posted by canyon289
Referring to the attached screenshot

The output of the function is saved as ANS when I want it saved as Y.
See how I inputted a variable for 1, ran my program and it didn't change the value of Y but rather added another scalar named ANS

Thanks for you help
y= stress(Ri,Ro,R,Delta,E)

CB

7. I don't understand. Do I put that in my function file somewhere? Sorry I'm very new to this.

8. Originally Posted by canyon289
I don't understand. Do I put that in my function file somewhere? Sorry I'm very new to this.
no you put it in the command line or top level script, with the input variable assigned to the values you want, or just use numeric values.