Hi, I need a function that calculates the sine of the corresponding element of an input. The input is in degrees and I am not allowed to use the built-in function sind.

11 次查看(过去 30 天)
Hi, I need a function that calculates the sine of the corresponding element of deg. Deg are giving in degrees and I am not allowed to use sind. My code is as follows:
function [value,average]=sindeg(deg)
value=deg*pi/180;
average=sum(value)/numel(deg)
end
I know the result of value should be 0 1 0 for deg = [0 90 180].
Mine function is clearly wrong. Can you help. Thanks

采纳的回答

Star Strider
Star Strider 2017-2-18
编辑:Star Strider 2017-2-18
Please think about it. A circle has 2*pi radians in its circumference (the units the sin function uses) and 360 degrees in its circumference. So:
2*pi radians = 360 degrees
I leave the rest for you.
As for writing the function, see the section on Anonymous Functions in the documentation on Function Basics (link).
EDIT Added ‘Function Basics’ link.

更多回答(2 个)

Image Analyst
Image Analyst 2017-2-19
编辑:Image Analyst 2017-2-19
So, can we assume you're not allowed to use sin() (the radians version) either?
One trivial way to do it (compute sine) is to use the known Taylor series expansion for sine. Simply multiply your degrees by the proper factor to get radians, then use the Taylor series. It's easily done with either a for loop, or vectorized.
  2 个评论
Image Analyst
Image Analyst 2017-2-19
Looks like you just wanted to use Star's hint of multiplying by the conversion factor to convert degrees into radians but that you were still allowed to use the built-in sin() function, which uses radians. For completeness, and possibly for others, I give code to compute the sine and cosine in degrees via a Taylor series expansion, then I compare it to the actual values from sind() and cosd() so you can check the accuracy. Don't be afraid of the length - that's just to make it fancy and well commented - the actual code engine is only 3 lines long.
% Code to computer the sine and cosine in degrees via a Taylor Series and compare it to the value from the sind() and cosd() functions.
clc; % Clear the command window.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Ask user for a floating point number.
defaultValue = {'67.5'};
titleBar = 'Enter degrees';
userPrompt = {'Enter the angle in degrees '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
xDegrees = str2double(caUserInput{1});
% Check usersValue1 for validity.
if isnan(xDegrees)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
xDegrees = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', xDegrees);
uiwait(warndlg(message));
end
% Convert degrees to radians, which the taylor series needs.
x = xDegrees * pi / 180;
% Compute the Taylor series iterator indexes as a vector
% so we can do the whole equation in a vectorized way.
k = 0 : 15 % 15 terms seems to be plenty.
% First do the sine via a Taylor series expansion.
terms = (-1) .^ k .* x .^ (2 * k + 1) ./ factorial(2 * k + 1)
sinTaylor = sum(terms)
message = sprintf('For x = %f degrees,\nthe actual sind(%f) = %8.4f,\nwhile the Taylor Series sin = %8.4f.\nThe difference = %.20f.',...
xDegrees, xDegrees, sind(xDegrees), sinTaylor, sind(xDegrees) - sinTaylor);
fprintf('%s\n\n', message);
uiwait(helpdlg(message));
% Then do the cosine via a Taylor series expansion.
terms = (-1) .^ k .* x .^ (2 * k) ./ factorial(2 * k)
cosTaylor = sum(terms)
message = sprintf('For x = %f degrees,\nthe actual cosd(%f) = %8.4f,\nwhile the Taylor Series cos = %8.4f.\nThe difference = %.20f.',...
xDegrees, xDegrees, cosd(xDegrees), cosTaylor, cosd(xDegrees) - cosTaylor);
fprintf('%s\n', message);
uiwait(helpdlg(message));

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-2-18
No-where in your code do you calculate sine

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by