How to get my code to define variables

5 views (last 30 days)
Marcus
Marcus on 28 Mar 2015
Edited: per isakson on 28 Mar 2015
Hi, I've written a quick piece of code which is the following:
function [Delta, theta, theta1, theta1prim, theta2prim, theta2] = mindeviation(n, A)
n0 = 1;
Delta = asin(n/n0 * sin(A/2))*2 - A;
theta = (Delta + A)/2;
disp(['Minimum deviation is ' num2str(Delta) ' radians or ' num2str(Delta*180/pi) ' degrees.'])
disp(['The angle of incidence is ' num2str(theta) ' radians or ' num2str(theta*180/pi) ' degrees.'])
theta1 = linspace(0,pi/2);
theta1prim = (asin(sin(theta1)/n));
theta2prim = (A-theta1prim);
theta2 = (asin(n*sin(theta2prim)));
Delta2 = theta1 + theta2 - theta1prim - theta2prim;
plot(theta1*180/pi,Delta2*180/pi)
xlabel('Angle of incidence (deg)')
ylabel('Angle of deviation (deg)')
xlim([0 90])
ylim([10 45])
I want it to define Delta, theta, and so on in Matlab when I run it but it doesn't for some reason so I figured I must be missing something important, but I can't find it whatever I try. Can anyone help me how to fix it? Thanks a lot for your time.
  1 Comment
per isakson
per isakson on 28 Mar 2015
Edited: per isakson on 28 Mar 2015
  • "doesn't for some reason" &nbsp how do you know it does not?
  • Do you know how to use breakpoints?

Sign in to comment.

Answers (2)

James Tursa
James Tursa on 28 Mar 2015
Edited: James Tursa on 28 Mar 2015
I assume you mean you want to capture the variables Delta, theta, etc in the workspace where mindeviation is called. You must do so explicitly. E.g.,
n = whatever
A = whatever
[Delta, theta, theta1, theta1prim, theta2prim, theta2] = mindeviation(n, A);
Now you should have Delta, theta, etc in your workspace.

Star Strider
Star Strider on 28 Mar 2015
You didn’t post any of your other code, so I can only guess you are not actually calling your function. In your main script, include this line:
[Delta, theta, theta1, theta1prim, theta2prim, theta2] = mindeviation(n, A);
You should have all the returned values in your MATLAB main script workspace. Your function doesn’t know you want it to run unless you tell it to.

Community Treasure Hunt

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

Start Hunting!