I need to substitute a value for s in a transfer function. For example:
G(s)= 1/ (s+3)
I need to substitute
s = -2.118 +2.221j
What code should I use for this?
PS: Unfortunately, I only have control system toolbox in MATLAB.
What's wrong with saving m-file with
function g = transferFun( s )
g = 1 ./ ( s + 3 )
And then calling the function
>> transferFun( -2.118 + 2.221*j )
As shai mentioned you can simply create an m file with the function.
However, if you are just doing some quick calculations here is a way to just do it on the command line. You can define an anonymous function like this:
G = #(s) 1/(s+3)
Now you can simply call it like this:
G(-2.118 +2.221j)
Note that Matlab is case sensitive.
Related
I'm using some user generated functions in MatLab. It'll be quicker if I don't post my actual code here so I'll summarize.
I have two functions. Each of them exist in their own files saved under their proper names. They can be called and work correctly. Lets say function1 is:
function [Output1] function1=(a,b)
Output1=a+b
end
function [Output2] function2=(a,Output1)
Output2=a+Output1
end
new script file
a=2;
b=3;
function1(a,b);
function2(a,Output1);
This doesn't work, because function1 isn't posting Output1 into the workspace. How do I make it do that?
Check this tutorial. This is how you are supposed to write a function.
function Output1 = function1(a, b)
Output1 = a + b;
end
Then your second function will get the input like this.
function Output2 = function2(a, Output1)
Output2 = a + Output1;
end
Of course you need to store answers of functions into variables to have them stored in the workspace.
aa=2;
bb=3;
Output11 = function1(aa,bb);
Output22 = function2(aa,Output11);
If you don't use Output11 and Output22, functions will store their result into ans variable in the workspace. And obviously, apart from the fact that you cannot pass the variable to the second function as Output1, second function will overwrite the ans.
I have a function which is very open-ended that I use in several different applications. Instead of changing it everytime, I would like to pass several command lines as input for the function to evaluate using something like the eval function.
Thus, for example, my argument would be:
str={'a=32;
b=a+3*a^2+pi;
c=sin(a)+cos(b)^2;'}
then I could call the function with str as an argument:
x=func(str)
these lines would be evaluated inside the function
how?
Thanks alot!
I think #Daniel is right that function handles are the way forward. Based on your example, this is how you'd do it:
function x = testfun( a, bfun, cfun )
b = bfun(a);
c = cfun(a, b);
x = a + b + c;
end
Then you'd call it like this:
x = testfun( 32, #(a)(a+3*a^2+pi), #(a,b)(sin(a)+cos(b)^2) );
Suppose the matrix is A which is in a m file new1.m . Now I want to access this matrix to another m file new2.m . How can it be done ?
Your question is a little nonspecific, but I'll try to answer.
There are several ways to do this,
Assuming that you have a m-file (script) named 'new1' with (for example) A = rand(4) in it. You could just run it in new2.m before you want to use A
new1;
B = 2*A;
Note that new1 will return all the other variables assigned in it, flooding your workspace. Perhaps not a problem, but if so, you could just clear them with
clear var1 var2 var2 etc.
Another way is to make new1 into a function and return (only) A
function A = new1()
but I'm guessing that might ruin some other purposes of new1.
In that case you could return A only if the function is called with a special input argument (for example 'getA')
function new1(varargin)
...
... % some code
...
if nargin && strcmp(varargin{1},'getA')
assignin('caller','A',A);
end
And so from new2, just call the function.
new1('getA');
I have been using MATLAB fminunc function to solve my optimization problem. I want to try the minFunc package :
http://www.di.ens.fr/~mschmidt/Software/minFunc.html
When using fminunc, I defined a function funObj.m which gives me the objective value and the gradient at any point 'x'. It also takes in several external inputs say, {a,b,c} which are matrices. So the function prototype looks like :
function [objVal,G] = funObj(x,a,b,c)
I want to use the same setup in the minFunc package. From the examples, I figured this should work :
options.Method='lbfgs';
f = #(x)funObj(x,a,b,c);
x = minFunc(f,x_init,options);
But when I call this way, I get an error as:
Error using funObj
Too many output arguments.
What is the correct way to call minFunc for my case?
**EDIT : Alright, here is a sample function that I want to use with minFunc. Lets say I want to find the minimum of a*(b-x)^2, where a,b are scalar parameters and x being a scalar too. The MATLAB objective function will then look like :
function obj = testFunc(x,a,b)
obj = a*(b-x)^2;
The function call to minimize this using fminunc (in MATLAB ) is simply:
f = #(x)testFunc(x,a,b);
x = fminunc(f,x_init);
This gives me the minimum of x = 10. Now, How do I do the same using minFunc ?
"Note that by default minFunc assumes that the gradient is supplied, unless the 'numDiff' option is set to 1 (for forward-differencing) or 2 (for central-differencing)."
The error is because only one argument is returned by the function. You can either return the gradient as a second argument or turn on numerical differencing.
Agree with Mark. I think the simplest way to solve it is
minFunc(#testFunc, x_init, a, b, c)
In MATLAB temporary function can only have one return value. So f = #(x)testFunc(x,a,b); let your method drop gradient part every time. Because minFunc can accept extra paramters, you can pass a, b and c after x_init. I think this would work.
I created an anonymous function inside a script and I can't get MATLAB to run the fminsearch? Here's an what I have so far:
V=x(1);
f=x(2);
q=#(x) (pi.*D.*L)./(1000.*V.*f);
fminsearch(#q,x);
The variables D and L are defined, but MATLAB gives me the following error:
Error: File: Testing.m Line: 51 Column: 17
"q" was previously used as a variable, conflicting with its use here as the name of a function or command.
See "How MATLAB Recognizes Command Syntax" in the MATLAB documentation for details.
q is not mentioned before this command. What am I doing wrong?
Another thing that could solve my problem is to get my script to write a function file, but how to do that?
Remove the second #:
V=x(1);
f=x(2);
q=#(x) (pi.*D.*L)./(1000.*V.*f);
fminsearch(q,x);
q is a function handle. fminsearch expects a function handle. You can create a function handle out of a function using an # (e.g. #min), but you don't need to do that here.
You can also write the anonymous function inline with the search command:
V=x(1);
f=x(2);
fminsearch(#(x) (pi.*D.*L)./(1000.*V.*f),x);
UPDATE (credits to #wakjah)
For your code to do anything sensible, you should use the argument x of the anonymous function:
x0 = [initialV, initialF];
fminsearch(#(x) (pi.*D.*L)./(1000.*x(1).*x(2)), x0);
#function creates a function handle for an existing function.
q = #(x) whatever... creates a function handle called q.
But, you can't create a function handle for a function handle, only for a function.
See this:
>> fones = #ones
fones =
#ones
>> ffones = #fones
Error: "fones" was previously used as a
variable,
conflicting with its use here as the name
of a function or command.
See MATLAB Programming, "How MATLAB
Recognizes Function Calls That Use
Command Syntax" for details.
In Matlab, a function handle is a kind of a pointer to a function and is distinct from a function (unlike in some other languages where the a function identifier can be passed and stored as any other variable).
It's important to note that calling a function and a function handle results in the same behaviour. Except for the case where the identifier is used without any parentheses following it:
>> ones
ans =
1
>> fones
fones =
#ones
>> ones(2)
ans =
1 1
1 1
>> fones(2)
ans =
1 1
1 1