error using sin or math.sin

조회 수: 9 (최근 30일)
Evone
Evone 2025년 1월 12일
답변: Umar 2025년 1월 13일
I have plugged in math.sin and sin into my equation and I continue getting errors when trying to run the script. I am lost and I am not sure how to correct this.
(8+6)*abs(4-15) + math.sin(math.log10(18))-(4+6) * math.tan(8/18+1)
Unable to resolve the name 'math.sin'.
(8+6)*abs(4-15) + sin*(math.log10(18))-(4+6) * math.tan(8/18+1)
Error using sin. Not enough input arguments.
  댓글 수: 1
Stephen23
Stephen23 2025년 1월 12일
편집: Stephen23 2025년 1월 12일
It looks like Python:
In any case:
(8+6)*abs(4-15) + sin*(math.log10(18))-(4+6) * math.tan(8/18+1)
% ^ what do you expect this asterisk to achieve?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2025년 1월 12일
(8+6)*abs(4-15) + sin(log10(18))-(4+6) * tan(8/18+1)
ans = 76.2282
math.sin looks like syntax for Python.

Umar
Umar 2025년 1월 13일

Hi @Evone,

When working with mathematical functions in MATLAB, it is essential to understand the syntax and the appropriate functions available in the environment. The errors you are experiencing stem from a few key misunderstandings regarding function calls and the use of the math namespace, which is not applicable in MATLAB.

Understanding the Errors

Error: Unable to resolve the name 'math.sin' * In MATLAB, there is no math namespace as you might find in Python. Instead, trigonometric functions like sin, cos, and tan are directly available without any prefix.

Error: Not enough input arguments * This error occurs when the function is called incorrectly. In your case, you attempted to use sin* instead of sin(), which is the correct syntax for function calls in MATLAB.

Correcting the Code

To resolve these issues, you need to rewrite your equation using the correct MATLAB syntax. Below is the corrected version of your original expression:

result = (8 + 6) * abs(4 - 15) + sin(log10(18)) - (4 + 6) * tan(8 / 18 + 1);

Breakdown of the Corrected Code

  • (8 + 6): This part calculates the sum of 8 and 6.
  • abs(4 - 15): The abs function computes the absolute value of the difference between 4 and 15, which is 11.
  • sin(log10(18)): The log10 function computes the base-10 logarithm of 18, and then sin computes the sine of that result.
  • (4 + 6): This calculates the sum of 4 and 6, which equals 10.
  • tan(8 / 18 + 1): This computes the tangent of the expression 8 / 18 + 1.

Final Result

To execute the code and obtain the final result, you can run the following complete MATLAB script:

% Calculate the result based on the provided expression
result = (8 + 6) * abs(4 - 15) + sin(log10(18)) - (4 + 6) * tan(8 / 18 + 1);
% Display the result
disp(result);

Please see attached.

By following the corrections outlined above, you should be able to run your MATLAB script without encountering the previous errors. Remember, always ensure that you are using the correct syntax for function calls in MATLAB, and avoid using namespaces that are not applicable in this environment. If you have any further questions or need additional assistance, feel free to ask!

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by