0

I'm pretty new to MATLAB and I have the following code:

a=500;
PI=3.14159265;
radconvert=PI/180;
degconvert=180/PI;

% values at t=9, t=10 and t=11 respectively
alpha=[54.80 54.06 53.34];
beta=[65.59 64.59 63.62];

% converting degrees to radians
for i = 1:3
    alpha(i) = alpha(i)*radconvert;
end

for i = 1:3
    beta(i) = beta(i)*radconvert;
end

% declaring x and y functions
function x(a, alpha, beta)
    (a*tan(beta))/(tan(beta)-tan(alpha));

function y(a, alpha, beta)
    (a*tan(alpha)*tan(beta))/(tan(beta)-tan(alpha));

% doing the calculations
vx1=x(a, alpha(2), beta(2)) - x(a, alpha(1), beta(1));
vy1=y(a, alpha(2), beta(2)) - y(a, alpha(1), beta(1));
vf1=sqrt(vx1^2 + vy1^2);

vx2=x(a, alpha(3), beta(3)) - x(a, alpha(2), beta(2));
vy2=y(a, alpha(3), beta(3)) - y(a, alpha(2), beta(2));
vf2=sqrt(vx2^2 + vy2^2);

vf=(vf1+vf2)/2;
vxavg=(vx1+vx2)/2;
vyavg=(vy1+vy2)/2;

theta = atan(vyavg/vxavg)*degconvert;

% printing the values
sprintf('Velocity at t=10 is: %.2f\n', vf);
sprintf('Climb angle theta is: %.2f\n', theta);

If I try to run it, I get the following error message:

??? Error: File: code.m Line: 20 Column: 1 Function definitions are not permitted in this context.

I guess I cannot declare and use functions in the same .m file. Can someone help me solve this problem?

3
  • possible duplicate of In MATLAB, can I have a script and a function definition in the same file? Commented Apr 15, 2013 at 22:13
  • Erkant, either place the functions in different files, rewrite the above script as a function, or use anonymous functions. Commented Apr 15, 2013 at 22:24
  • One more problem I see, you are not returning any value from the function but the LHS of the function call is assigned to vx1, vy1 etc. That won't work. Commented Apr 15, 2013 at 22:45

1 Answer 1

4

You are not permitted to define functions inside scripts. Those are two different things. Either your .m file is a function of a script. You can however define anonymous functions in MATLAB scripts. Because of scoping issues that I will not expand on here, your function arguments should not have the same names as the variables in your main script. Their scope intersects. If the previous sentence does not make sense, just make sure that your argument names are different from what you called things in your script. Your code with ananymous functions should look like this:

a=500;
PI=3.14159265;
radconvert=PI/180;
degconvert=180/PI;

% values at t=9, t=10 and t=11 respectively
alpha=[54.80 54.06 53.34];
beta=[65.59 64.59 63.62];

% converting degrees to radians
for i = 1:3
    alpha(i) = alpha(i)*radconvert;
end

for i = 1:3
    beta(i) = beta(i)*radconvert;
end

% declaring ananymous x and y functions
x = @(mA, mAlpha, mBeta) ...
    (mA*tan(mBeta))/(tan(mBeta)-tan(mAlpha));

y = @(mA, mAlpha, mBeta) ...
    (mA*tan(mAlpha)*tan(mBeta))/(tan(mBeta)-tan(mAlpha));

% doing the calculations
vx1=x(a, alpha(2), beta(2)) - x(a, alpha(1), beta(1));
vy1=y(a, alpha(2), beta(2)) - y(a, alpha(1), beta(1));
vf1=sqrt(vx1^2 + vy1^2);

vx2=x(a, alpha(3), beta(3)) - x(a, alpha(2), beta(2));
vy2=y(a, alpha(3), beta(3)) - y(a, alpha(2), beta(2));
vf2=sqrt(vx2^2 + vy2^2);

vf=(vf1+vf2)/2;
vxavg=(vx1+vx2)/2;
vyavg=(vy1+vy2)/2;

theta = atan(vyavg/vxavg)*degconvert;

% printing the values
msg = sprintf('Velocity at t=10 is: %.2f\n', vf);
disp(msg);
msg = sprintf('Climb angle theta is: %.2f\n', theta);
disp(msg);

I also fixed your printing at the end.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.