how to show the linearization result in Dymola? - modelica

I tried to use the linearization function in Dymola, but it seems when the result's dimension is large, Dymola won't show the result.
My question is:
How could I print the result or where to find it?

What you can do, is assign the result to a variable. This can be done using the Outputs group as shown in the screenshot below. If you e.g. enter "sys" in the field for ss, you will get a record sys, in which you can access the matrices/vectors by typing sys.A, sys.B etc., which I've tested for a system of size 200x200. Typing this into the command line will display the content. Of course this record not only for outputting it, but also for post-processing.
The only thing this actually does, is modify the call from Modelica_LinearSystems2.ModelAnalysis.Linearize("ModelName") to sys=Modelica_LinearSystems2.ModelAnalysis.Linearize("ModelName"), so it can be done in the Commands window as well.

Call the function from the command line and capture the output. Then you can do with it what ever you want.
Everything you find in the Linear Analysis toolbar is part of the library Modelica_LinearSystems2. The Linearize item in this menu calls the function
Modelica_LinearSystems2.ModelAnalysis.Linearize("<your-model>")
which is also printed to the command line. The function returns the operator record Modelica_LinearSystems2.StateSpace, which contains all the info you are interested in. The default behavior of Dymola is to call the String method of this operator record and print it to the command line. If you look at the source code of Modelica_LinearSystems2.StateSpace.'String' you can see this at the start of the algorithm section:
// If system is too large, do not print the matrices
if size(ss.A,1) > 50 or size(ss.B, 2) > 50 or size(ss.C, 1) > 50 then
...
On the command line you can capture the operator record in a variable like this:
stateSpace = Modelica_LinearSystems2.ModelAnalysis.Linearize("<your-model>");
And then access the values on the command line via
stateSpace.A
stateSpace.B
stateSpace.C
stateSpace.D
For a nice html report you can also pass the operator record to one of the analysis functions:
Modelica_LinearSystems2.StateSpace.Analysis.analysis2.printSystem(stateSpace)
This creates the file systemAnalysis.html in your working directory, containing a nice visual presentation of your system.

Related

How do I resolve an "Invalid use of operator" error in an input function?

I am writing a code that requires the user to input a specific file directory (which they can obtain by hitting copy address as text when in the corresponding file). The line of code looks like this and it occurs near the very beginning of the code:
folder = input('Input file location (Copy address as text) \n(i.e. C:\\Users\\joahf\\Documents\\MATLAB\\Image Processing\\image_sets\\software_setup): '); % Have user input file location.
When I run the code, this is what I input and the corresponding error that occurs:
The code ends instantly after submitting this input.
The code continues to run fine when I do not input anything and the input functions returns an empty matrix. Even stranger, when I input something simpler such as "hello", I get a different error and it allows me to reenter it:
If someone knows what could be going on that would be much appreciated.
Aisde: You should consider using uigetfile or uigetdir which are better suited to inputting file paths than input.
To answer the actual question, input evaluates the input string as code (i.e. pipes it into eval), which makes it pretty dangerous for anything "customer-facing". From the docs:
x = input(prompt) displays the text in prompt and waits for the user to input a value and press the Return key. The user can enter expressions, like pi/4 or rand(3), and can use variables in the workspace.
If the user presses the Return key without entering anything, then input returns an empty matrix.
If the user enters an invalid expression at the prompt, then MATLABĀ® displays the relevant error message, and then redisplays the prompt.
Emphasis mine, the final sentence shows exactly what you're seeing, it is either trying to evaluate a file path as code and erroring with an invalid operator usage (likely referring to the colon near the start or one of the slashes which aren't doing what they normally do in MATLAB) or erroring and re-prompting when you enter something without an operator (like hello).
You need to use the 2nd usage example from the docs:
txt = input(prompt,'s') returns the entered text, without evaluating the input as an expression.
You can use "s" (string) or 's' (char) equivalently.

How to change obtained format

I've got a problem changing my code result. The main problem is when I run the code, it works very well, and the final results are accurate. However, as you can see it's like a division of 2 big numbers. Please help me out how to change the formation of the results. I have to say that I've already used **format command ** and didn't get anything.
the result I want is something like :
sigma=156.45e+6;
Thanks.
The format command isn't what you need here I think.
If these values were generated using the symbolic toolbox, then they tend to remain as whole fractions, and in order to change this, you simply need to run the following code either in your script or in the command window:
sympref('FloatingPointOutput',true);
This will produce the values you are looking for.
Alternatively you can cast the values you have to a double using the following code:
ans = double(ans);
sigma1 = double(sigma1);
sigma2 = double(sigma2);
You must have set the command window format to long some time ago as this is not the usual behavior.
You can change this simply by typing to the command window.
The defaults are:
format shortEng % number representation
format compact % line spacing
BTW, you can get your current setting with this command
get(0,'Format')
However, changing the format only applied to your current session (until you close MATLAB). Therefore it is odd that you ask. If it persists to be changed, someone must have fiddled with the preference
The specified format applies only to the current MATLAB session. To maintain a format across sessions, choose a Numeric format or Numeric display option in the Command Window Preferences.

matlab 'evidence removio': delete last shown command

I would like to remove whatever I just typed - and the last shown command - from the matlab console display. Needless to say, this would be ideal for pranksters (but this is of course strictly for academic purposes only). This is as far as I have gotten (based on this related answer):
hist = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory; %get history
last = strjoin(cell(hist(end-2:end)),' '); %convert history to string
fprintf(repmat('\b',1,numel(last))); %replace characters of string with whitespace
However I can only access the last typed command (through the command history) - not the last displayed command (which would be ideal). Any ideas how to solve this?
Disclaimer: I don't advise doing this.
The MATLAB CommandWindow contents are stored as a CmdWinDocument, which is an extension of the Java PlainDocument type, and an interface of the Document type. The current window can be accessed using the command:
com.mathworks.mde.cmdwin.CmdWinDocument.getInstance
In theory, you should be able to remove text from the command window using something like:
doc = com.mathworks.mde.cmdwin.CmdWinDocument.getInstance
endpos = doc.getEndPosition
doc.remove(endpos-10,10)
which would, in theory, remove the last 10 character from the document. You may have to call the removeUpdate function as well. Obviously problems will be caused by the fact that these commands will be appended to the document during this process. I have not tested this, and you're likely to cause problems with internally stored offsets within the CmdWinDocument class, so use at your own risk.

Retrieve past results

Is it possible to retrieve previous results in the command window? For example i had some matrices and vectors in the command window and after an error the matlab was closed . Is it possible to retrieve them?
Thanks
No guarantees about what happens after a crash, but in general, use the command diary('filename.txt') to append all text in the CommandWindow -- not the Command History window -- to that text file.
(I dunno why MathWorks doesn't call the "CommandWindow" what it is, i.e. the Console, but there you are.)

How to invoke a MATLAB script in runtime by its name

Suppose I have 3 m-codes:
code1.m code2.m code3.m
and I want a code for MATLAB to "draw them together" in the sense that when we run the program, we are prompted with, say, "enter code:", then the user types in say "code3" and then code3.m is run.
I am pretty sure there is a simple code to do that, though I can't remember it.
There are two portions to this question, the first of which is getting user input:
Matlab allows you to request user input as shown in this tutorial: http://www.mathworks.com/help/techdoc/ref/input.html
strResponse = input(prompt, 's')
Part two is simply loading the file and executing it, as described by #MetalRain
http://www.mathworks.com/help/techdoc/ref/eval.html
eval(['load code' strResponse '.m'])
Noting that matlab perform string concatanation on the vector for you, so the result for the input of strResponse = 1 is 'load code1.m'
run or eval can do it. You get the name of the file from input.
A (maybe) less flexible but safer method is to use the graphical version of input named inputdlg.