help with asin and acos

2 views (last 30 days)
carlos landin
carlos landin on 1 Mar 2016
Edited: James Tursa on 2 Mar 2016
Create a function called my_asin that accepts a single value of x and check whether this between -1 and +1 (-1 < = x < = 1). If x is out of range of range , I sent an error message to the screen. If you are in the allowable range , return the value of asin and acos
  2 Comments
James Tursa
James Tursa on 1 Mar 2016
What have you done so far? Please post your code with specific questions and we can advise and provide help. Do you know how to write a function? Do you know how to write an if-test to see if x is within the desired range? Do you know how to send an error message?
carlos landin
carlos landin on 1 Mar 2016
Edited: James Tursa on 2 Mar 2016
thanks but i already done this just read a bit and thats it this is the final code
unction [n1]=my_asin(valor)
n1=valor;
if n1>1
fprintf( Fuera de Rango')
else
resultado1=asind(n1)
resultado2=acosd(n1)
end
end

Sign in to comment.

Answers (1)

James Tursa
James Tursa on 2 Mar 2016
Edited: James Tursa on 2 Mar 2016
You made a decent start so I will offer some suggestions/corrections.
You are using n1 for both the input and the output. Don't do that. Use it for one or the other, but not both. (Technically you could use it for both if you wrote the code properly, but in this case it would just be a confusion factor so I don't advise it)
The check n1>1 only checks to see if the input is greater than 1. It does not also check to see if the input is less than -1. So maybe use the abs function here to check for both conditions.
Did your professor intend for you to write two functions? I.e., my_asin and my_acos? It would be weird if my_asin returned both an asin and acos value.
To throw an error use the error function, not the fprintf function. That way the program will actually stop at that point and not try to continue with bad data.
E.g., here is one way to adjust your code as mentioned above for my_asin:
function [n1]=my_asin(valor)
if abs(valor) > 1 % checks both <-1 and >1 condition
error('Fuera de Rango') % stops execution with an error
else
n1 = asind(valor); % computes the result and places it in the return variable
end
end
And then make another completely separate function called my_acos that is similar (changes are trivial and should be obvious).
You should also place some comments at the front of your code that state inputs and outputs and any restrictions in usage. E.g., something like this:
% my_asin computes asin with input range checking
% Input: valor = real scalar between -1 and 1
% Output: n1 = asind(valor) , i.e., returned result is in degrees

Products

Community Treasure Hunt

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

Start Hunting!