MATLAB, function that evaluates sinh, cosh, tanh

  • Thread starter Thread starter btbam91
  • Start date Start date
  • Tags Tags
    Function Matlab
AI Thread Summary
The discussion focuses on creating a MATLAB function that evaluates hyperbolic functions sinh, cosh, and tanh based on a string input. The user has written a script that defines these functions as sub-functions and uses a switch statement to select which function to evaluate. A key issue arises when trying to call the main function with the function name without quotes, resulting in an error. The solution provided suggests using quotes around the function name when calling the main function, such as myhyperbolic8_17('sinh',2). This adjustment resolves the argument error and allows the function to work as intended.
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
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
20
Views
2K
Replies
10
Views
2K
Replies
3
Views
1K
Replies
6
Views
5K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Back
Top