MATLAB Answers

How can I write a custom complex mathematical expression?

1 view (last 30 days)
KUMAR TRIPATHY
KUMAR TRIPATHY on 2 May 2021
Answered: William Rose on 2 May 2021
I want to write this mathematical expression in matlab which is a function of 3-4 variables,
Can Anyone help me how can I write such complex functions and evaluate these functional values for different input values?
Also please suggest any mathwork documents to read for writing these complex functions!
Thank you!!!

Answers (1)

William Rose
William Rose on 2 May 2021
I recommend that you read the Matlab help on writing functions.
Here is an example of how you can create the function to do what you want. Write this as a script, and save it with the name myFunction.m (should match the name on the right hand side of line 1).
Here is an example of two calls to the funciton. The first call results in a warning, because one of the conditions is not met. The second call works fine.
>> m1=1; a1=2; b1=3; k1=4;
>> y=KumarsFunction(m1,a1,b1,k1);
Error: When m<>0, 1+k*(m-a)/b must be positive.
>> m1=4; a1=3; b1=2; k1=1;
>> y=KumarsFunction(m1,a1,b1,k1);
>> fprintf('y=myFunction(%.1f,%.1f,%.1f,%.1f)=%.3f\n',m1,a1,b1,k1,y);
y=myFunction(4.0,3.0,2.0,1.0)=0.513
Here is the function you asked about. I assume from your definition that beta must be >0. Therefore I check for this, and if beta is not >0, an error message is issued, and the function returns the value -1. Obviously you could change this.
function x=KumarsFunction(m,a,b,k)
%Function for Kumar.
%Inputs: m, a, b, k. Error if b<=0.
if b<=0
disp('Error: b must be >0.');
x=-1;
return
end
if m==0
x=exp(-exp((a-m)/b));
else
if 1+k*(m-a)/b<0
disp('Error: When m<>0, 1+k*(m-a)/b must be positive.');
x=-1;
return
end
x=exp(-((1+k*(m-a)/b)^(-1/k)));
end
end
Try it.

Community Treasure Hunt

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

Start Hunting!