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 件のコメント
per isakson
2015 年 3 月 28 日
編集済み: per isakson
2015 年 3 月 28 日
- "doesn't for some reason"   how do you know it does not?
- Do you know how to use breakpoints?
回答 (2 件)
James Tursa
2015 年 3 月 28 日
編集済み: James Tursa
2015 年 3 月 28 日
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
2015 年 3 月 28 日
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!