Linearize nonlinear state space equation in MATLAB at steady state setpoint
This post shows one way to linearize a nonlinear state equation at a steady state setpoint
in MATLAB. It is assumed that a function
ode.m exists in which the state equation is implemented:
1 2 3 4 5 6 7 8 9 10 |
function dx = ode(t, x, u) % example ODE dx1 = tan(x(4)) * x(2) + 2*u(2) - 1; dx2 = x(1) - sin(x(2)); dx3 = 13 * x(4) + u(1) + 1; dx4 = 2*x(1); dx = [dx1; dx2; dx3; dx4]; end |
Calculate Levenshtein distance in MATLAB (words or characters) with Wagner–Fischer algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
function y = lvdistance(sentence1, sentence2, penalties, split_characters) %LVDISTANCE Calculate Levenshtein distance between two strings % Uses Wagner–Fischer algorithm % See https://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm % % Arguments: % sentence1, sentence2: strings, e.g. 'hello world' % penalties (optional): vector of penalties for deletion, insertion and % substitution (default: [1 1 1]) % split_characters (optional): split at each character, not only at spaces % (default: false) % % GPL v3, Nicolai Spohrer <nicolai@xeve.de> switch nargin case 2 penalty_deletion = 1; penalty_insertion = 1; penalty_substitution = 1; split_characters = false; case 3 penalty_deletion = penalties(1); penalty_insertion = penalties(2); penalty_substitution = penalties(3); split_characters = false; case 4 penalty_deletion = penalties(1); penalty_insertion = penalties(2); penalty_substitution = penalties(3); otherwise error 'Invalid number of arguments'; end if split_characters % split at each character sentence1 = num2cell(sentence1); sentence2 = num2cell(sentence2); else % split at spaces sentence1 = strsplit(sentence1, ' '); sentence2 = strsplit(sentence2, ' '); end m = length(sentence1); n = length(sentence2); % distance 2D array d = zeros(m+1, n+1); for i=1:m+1 d(i, 1) = i-1; end for j=1:n+1 d(1, j) = j-1; end for j=1:n for i=1:m if strcmp(sentence1{i}, sentence2{j}) % words are equal, therefore no operation required d(i+1, j+1) = d(i, j); else % words are not equal d(i+1, j+1) = min(min(... d(i, j+1) + penalty_deletion,... % deletion d(i+1, j) + penalty_insertion),... % insertion d(i, j) + penalty_substitution); % substitution end end end y = d(m+1, n+1); end |
Convert dSPACE ControlDesk measurements to MATLAB/Simulink timeseries (updated)
- With this updated MATLAB script, multiple dSPACE ControlDesk measurements can be imported with a single execution
- The result is saved as a tscollection (collection of timeseries) and all signals are saved with their corresponding name
- Access imported data with Simulink ‘From Workspace’ block and, e.g., dsp_tscs{1}.my_signal
Fix for Latex4CorelDraw with CorelDraw 2017
I was not able to make the official build of Latex4CorelDraw work with CorelDraw 2017 (and Windows 10). The ‘Latex’ Docker window was always empty. The solution was to recompile it with Visual Studio 2017 Community.
Convert dSPACE ControlDesk measurement to MATLAB timeseries that can be used in Simulink
- dSPACE ControlDesk can export measurements to mdf4 files or mat files
- mat file exports can be converted to timeseries with this MATLAB script
- The timeseries can then be imported into Simulink with the ‘From Workspace’ blog
- Multiple signals can be imported at once
Example output:
1 2 3 4 5 6 7 |
>> import_dspace_mat_to_simulink_ts --Imported dSPACE mat file C:\Users\Nicolai\Nextcloud\foo.mat into workspace variable dsp_ts --Time: 0.000000 s to 42.399572 s, 423980 steps --2 variables are in dsp_ts: 'In1' 'In1_1' |
Robust sync between Android phone and Linux computer
I already tried quite a few methods to reliably sync data (e.g. backups) between my Android phone and a Linux computer. Most were not good, some were horrible (MTP…). I just tried one that has proven to be the best, by far: installing an SSH server on the phone and then syncing with rsync. I
Django DetailView with pagination for related objects
This is an example of a DetailView of a User that can have a paginated list of Purchases:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from django.views.generic.detail import DetailView from django.core import paginator @method_decorator(staff_member_required(login_url='user_login'), name='dispatch') class UserDetailView(DetailView): model = User template_name = "user_detail.html" purchases_paginate_by = 5 def get_context_data(self, **kwargs): context = super(UserDetailView, self).get_context_data(**kwargs) purchases_page = self.request.GET.get("purchases_page") purchases = self.object.get_purchases().filter() purchases_paginator = paginator.Paginator(purchases, self.purchases_paginate_by) # Catch invalid page numbers try: purchases_page_obj = purchases_paginator.page(purchases_page) except (paginator.PageNotAnInteger, paginator.EmptyPage): purchases_page_obj = purchases_paginator.page(1) context["purchases_page_obj"] = purchases_page_obj return context |
In the template file, the pagination needs to done similar to the following example (using django-bootstrap3):
1 |
{% bootstrap_pagination purchases_page_obj extra=request.GET.urlencode parameter_name="purchases_page" %} |
Export object_list of a django-filters FilterView to CSV
This is a simple way to allow users to export the data they have just filtered using django-filter as CSV.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Normal View (has a link to export view and keeps GET parameters) @method_decorator(staff_member_required(login_url='user_login'), name='dispatch') class PaymentListView(FilterView): filterset_class = filters.PaymentFilter template_name = "barsys/userarea/payment_list.html" paginate_by = 10 # Export View @method_decorator(staff_member_required(login_url='user_login'), name='dispatch') class PaymentExportView(FilterView): filterset_class = filters.PaymentFilter def render_to_response(self, context, **response_kwargs): # Could use timezone.now(), but that makes the string much longer filename = "{}-export.csv".format(datetime.datetime.now().replace(microsecond=0).isoformat()) response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename) writer = csv.writer(response) writer.writerow(['created', 'email', 'display_name']) for obj in self.object_list: writer.writerow([obj.created_date, obj.user.email, obj.user.display_name]) return response |
Running multiple Django projects on one Apache instance with mod_wsgi
I did not yet find a page with a concise but complete description of how to run multiple Django instances/projects on one server with Apache. This method is similar to the one in the Django docs, but the docs version does not run the Django instance in a daemon process, therefore it does not work
Analytical convolution integral (Analytische Faltung) with Matlab and Maple
With and
Function 1 (e.g. input signal/Eingangssignal):
Function 2 (e.g. impulse response/Stoßantwort):