MATLAB, function that evaluates sinh, cosh, tanh

In summary, the conversation discusses creating a function that incorporates the previously created functions for sinh, cosh, and tanh. The function requires a string input for the function name and a value of x to evaluate the function at. The script provided includes subfunctions for each hyperbolic function and a switch statement for the parent function. The individual is experiencing an error message and is seeking assistance on how to properly call the function with a string input.
  • #1
btbam91
91
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
  • #2
Try:

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

1. What is MATLAB?

MATLAB is a high-level programming language and interactive environment used for scientific computing, data analysis, and algorithm development. It is widely used in various fields such as engineering, mathematics, finance, and biotechnology.

2. What is a function in MATLAB?

A function in MATLAB is a group of commands that perform a specific task. It takes input arguments and produces output results. Functions help in organizing code, making it more modular and reusable.

3. What are sinh, cosh, and tanh in MATLAB?

These are mathematical functions in MATLAB that calculate the hyperbolic sine, cosine, and tangent of a given input. They are useful in solving mathematical problems involving exponential functions.

4. How do I use the sinh, cosh, and tanh functions in MATLAB?

To use these functions, you need to first define the input values, either as variables or directly in the function call. For example, to calculate the hyperbolic sine of 2, you can use the command "sinh(2)".

5. Can the output of these functions be used in further calculations?

Yes, the output of these functions can be used in further calculations, just like any other variable in MATLAB. You can assign the output to a variable and use it in other functions or equations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
811
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Special and General Relativity
2
Replies
36
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Special and General Relativity
3
Replies
75
Views
3K
  • Advanced Physics Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top