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.

조회 수: 7 (최근 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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by