How can I write a custom complex mathematical expression?
2 次查看(过去 30 天)
显示 更早的评论
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!!!
0 个评论
回答(1 个)
William Rose
2021-5-2
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!