How to get my code to define variables

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 个评论

  • "doesn't for some reason" &nbsp how do you know it does not?
  • Do you know how to use breakpoints?

请先登录,再进行评论。

回答(2 个)

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.
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.

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

提问:

2015-3-28

Community Treasure Hunt

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

Start Hunting!

Translated by