MATLAB, function that evaluates sinh, cosh, tanh

  • Thread starter Thread starter btbam91
  • Start date Start date
  • Tags Tags
    Function Matlab
Click For Summary
SUMMARY

The discussion focuses on creating a MATLAB function named myhyperbolic8_17 that evaluates hyperbolic functions: sinh, cosh, and tanh. The function requires two arguments: a string indicating the function name and a numeric value for evaluation. The user initially encountered an error due to incorrect input format, which was resolved by using the string representation of the function name, such as 'sinh', instead of passing the function itself.

PREREQUISITES
  • Basic understanding of MATLAB syntax and functions
  • Familiarity with hyperbolic functions: sinh, cosh, tanh
  • Knowledge of MATLAB's error handling with nargchk
  • Experience with MATLAB's switch-case control structure
NEXT STEPS
  • Explore MATLAB function creation and scope of nested functions
  • Learn about MATLAB's string handling and input validation techniques
  • Investigate error handling in MATLAB using try-catch blocks
  • Study optimization techniques for MATLAB functions to improve performance
USEFUL FOR

MATLAB developers, students learning numerical methods, and anyone interested in implementing mathematical functions in MATLAB.

btbam91
Messages
91
Reaction score
0
Hey guys, my problem asks me to create one function, the incorporates the 3 functions (sinh, cosh, and tanh) that I created in the previous problem.

The function requires 2 arguments:

1. A string containing the function names 'sinh', 'cosh', 'tanh'
2. the value of x at which the function should be evaluated at


Here's my script thus far.


function result = myhyperbolic8_17('name',x);
%Purpose: Create a function that evaluates sinh, cosh and tanh, having all
%three of them as sub functions.

msg= nargchk(2,2,nargin);
error(msg);

function sinh = sinh1(x)

%State equation
sinh = ((exp(x) - exp(-x))/(2));
end

function cosh = cosh1(x)

cosh = ((exp(x) + exp(-x))/(2));
end

function tanh = tanh1(x)

tanh = ((exp(x) - exp(-x))/(exp(x) + exp(-x)));
end



%Initiate a switch for the parent, myhypberbolic8_17 function

switch (name)
case 'sinh'
result = sinh1(x);
case 'cosh'
result = cosh1(x);
case 'tanh'
result = tanh1(x);
otherwise
disp('Argument Error')
end
end


I guess the first problem is, how do I make it so that the overall function accepts a string as an input?

But when I do run this script with say:

myhyperbolic8_17(sinh,2)

I get an error message saying that sinh does not have enough arguments.




Thanks!
 
Physics news on Phys.org
Try:

Code:
myhyperbolic8_17('sinh',2)
with quotes
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K