How to implement a .m script in Simulink? [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i have written a m-function in a script. This function script simulates behavior of a system.
Now i want to implement it in a Simulink Modell. The function has several inputs and several outputs. Actually i find the user-defined functions, but they all have one input and one output.
Do somebody now how i can implement the m.file into the simulink modell with more than one inputs and outputs?
Thank You!

All of the user defined functions allow multiple inputs.
For instance, the MATLAB Function block has a default of:
function y = fcn(u)
y = u;
Which can be changed to have 2 outputs and 3 inputs (for instance) just like any other MATLAB function:
function [out1,out2] = fcn(in1,in2,in3)
out1 = in1;
out2 = in2 + in3;

Related

why do i get fminsearch undefined function error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to optimize a function with 2 inputs. Trying to use fminsearch but it keeps saying undefined function or variable although it is already defined.
I have already defined the function in a separate script that is in the same directory with my main script. I have a classroom license which includes optimization toolbox and there is no spelling mistake while calling the function.
function o=u(x,y)
%some code here
end
%in a second script
init=[0.1,0.1];
b=fminsearch(o,init);
The error is:
Undefined function or variable 'o'.
From the documentation on fminsearch, the function being minimized must have a single argument and accessed with a function handle (see this related answer).
The error you are getting is because you can't call o and use it as an input to fminsearch() since o is undefined. To get o, you have to first get u(x,y), plus as mentioned, fminsearch requires a function handle as an input.
You have several options that still use your standalone function, u(x,y).
1. Create a function handle
Define a function handle that calls u(x,y) but has a single argument which is a 2 x 1 vector, z = [x; y].
fh =#(z) u(z(1),z(2));
z0 = [1; 2]; % Initial Guess for z = [x; y]
[z,TC] = fminsearch(fh,z0)
2. Change the function and call it directly
The same result is possible with
[z,TC] = fminsearch(#u,z0)
if you redefine u(x,y) to be as follows:
function o=u(z)
x = z(1);
y = z(2);
% your function code here
end

How to define a function with a matrix [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to define a two variables function g so that g(x,y) be a 2*2 matrix. To do this, I define g(x,y)=[1,1;x,y] but when I put g(1,1) I don't get any answer. How can I evaluate to g?
The code g(x,y)=[1,1;x,y] itself will not do anything. I assume that your expect result will be g=[1,1,1,1]? Therefore you should do as follow:
g=g_func(1,1);
disp(g)
function g=g_func(x,y)
g=[1,1;x,y];
end
It's not that different from the previous answer, but perhaps an anonymous function would meet your needs:
>> g = #(x,y)[1,1;x,y];
>> g(5,6)
ans =
1 1
5 6
Alternatively, if you want g to accept only one input (i.e. a 2-element vector, instead of two scalars), you could do:
g = #(x)[1,1;x(1),x(2)];
% or
g = #(x)[1,1;x(:).'];

matlab draw a line using user input values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I want to draw a line using the inputs for values x1,y1 and x2,y2 from the gui edit text box and plot them on the axes.
function
You are trying to convert the graphics handle itself to a number rather than converting the contents of the uicontrol to a number. To get the value, you'll want to use the 'String' property of the uicontrol instead.
x1 = str2double(get(handles.edit1, 'String'));
You will want to do the same for all user-supplied values.

Creating a mean square error function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I'm really new to MATLAB and have to implement some functions. To start, I'm supposed to make a function about a mean square error, for which I have the following formula:
My goal is to implement this in MATLAB. I've tried to analyse this for days, but I'm still lost. I guess the first thing to do is to create a function and then put all of the calculations. Should I declare variables like N though? Or can I just use them in the calculations without declaring? I'm just looking for some advises that would help me get started, I have no one else to ask about this, so a short guide/tips on how to take down this particular example would be amazing. Thank you in advance!
For a mean square error you need two inputs, Y and Y_bar and one output E. You don't need to declare N because it is implied by the length of each of your inputs. I'm going to assume that your inputs are both column vectors and are the same length.
function E = MSE(Y, Y_bar)
N = size(Y,1);
E = sum((Y-Y_bar).^2)/N
end
You should save this code a an .m file named mse.m and make sure it is in your working directory. If you don't know what that means, you need to look it up.
This is extremely basic MATLAB though, before you continue with anything, I think you should start by working through some beginners guides. As mentioned, stackoverflow is not the place to learn the very basics of a programming language.

Can Matlab optimize an external process? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm considering buying Matlab Home + Optimization module for home use, however I'm not sure it can do what I want it to.
I have an external process (not Matlab) that takes input, runs a process, and produces output. I want to tie in the input and output to Matlab so that Matlab can "optimize" these inputs, completely blind to the discrete process itself. Does Matlab have discrete optimization capabilities, or do all of its optimization functions rely on having internal access to the process itself?
Thanks!
-Stephen
if your external process is capable to assimilate parameters and give response to a external program using any methods eg, command line, or files, yes it is possible just configure your objective function to send and read the parameters and response data to the external process.
For the discrete optimization, the optimization toolbox do not work with discrete optimizations problems, but the documentation give a hint about rounding the parameter inside the objective function and then running again in the responses variable.
for example, this can be a function to optimize a volume of a prism
which is coded in a external program written in python (just for demonstration purpose with single objetive genetic algorithm (ga)):
function f = optim(x)
%Optimization criteria
l = round(x(1));
h = round(x(2));
w = round(x(3));
%String to produce the external proccess call as a system command
commandStr = ['python -c "print ' num2str(l) ' * ' num2str(h) ' * ' num2str(w) ' "'];
%Execute the system command, status = 0 for good execution
[status, commandOut] = system(commandStr);
%Convert the output of the external program from strin to doble and assign as the response of the optimization funcition
f = str2double(commandOut)
Then you can use the optimtool using this funcion as objetive as:
Then export the result to workspace and round() it.
Or make it programmable with a code like this:
function [x,fval] = runOptimization(lb,ub)
options = gaoptimset;
options = gaoptimset(options,'Display', 'off');
[x,fval] =ga(#optim,3,[],[],[],[],lb,ub,[],[],options);
x = round(x)
fval = optim(x)
And run as
[x,fval] = runOptimization([1 1 1],[3 4 5])
NOTE. the round() functions its only to demonstrate how to do discrete optimization as suggested in the documentation