Simulating block diagram with script file

2 views (last 30 days)
I tried to simulate a block diagram with Matlab code but it gives me wrong results, the block diagram is shown in the uploaded picture and the Matlab code is shown below
clear all;close all;clc % define the global variable global max centx centy p1 p2 p12 p21 root wx ax cx wy ay cy h global uxsave uysave tsave
uxsave=[]; uysave=[]; tsave=[];
%Nonlinear map parameters max = 1; % max value of nonlinear map centx = -1; % x-coordinate of center of nonlinear map centy = -1; % y-coordinate of center of nonlinear map p1 = 1; % values in Hessian (P) p2 = 0.5; p12 = 0; p21 = 0; root = 1; % can be used to spread map
% Extremum Seeking parameters wx = 20; % x-loop perturbation frequency ax = .05; % x-loop perturbation amplitude cx = 20; % x-loop gain
wy = 20; % y-loop perturbation frequency ay = .05; % y-loop perturbation amplitude cy = 20; % y-loop gain
% filter h=1;
% simulation t0=0; % start time t1=30; % end time
%initial condition of the state x0=[1,1,0];
% solving the ode tspan=[t0 t1]; [t,x]=ode23('pointmass_derivatives',tspan,x0);
%%%%%%%%%%%%%%%%%% for i=1:length(t) idx = find(tsave-t(i)==0); ux(i)=uxsave(idx(length(idx))); uy(i)=uysave(idx(length(idx))); end;
% trajectory of the states xp=x(:,1); yp=x(:,2); J=max./(1+p1.*(xp-centx).^2+p2.*(yp-centy).^2).^(root);
figure plot(t,J,'b','LineWidth',3); xlabel('time (sec)','FontSize',18,'FontWeight','bold'); ylabel('output','FontSize',18,'FontWeight','bold'); title('Objective Function','FontSize',18,'FontWeight','bold'); grid on; set(gca,'FontSize',12,'FontWeight','bold');
figure plot(xp,yp,'r','LineWidth',3); xlabel('x','FontSize',18,'FontWeight','bold'); ylabel('y','FontSize',18,'FontWeight','bold'); grid on; set(gca,'FontSize',12,'FontWeight','bold');
the mousa_derivatives function is as follows
mousa_derivatives
Is there anyone have an idea why the results are not correct?
Many thanks
  1 Comment
Cam Salzberger
Cam Salzberger on 2 Sep 2015
Hello Abdulmajid,
I am having a difficult time understanding your code, and what you are trying to do with it. Could you please edit the question so that the linebreaks are in the correct locations? You will find the "Code" button and "Preview" pane useful for this. Also, what data are you getting out of your code, and why do you believe it to be incorrect?
You may wish to use a function handle instead of a string to pass the function into ode23. This way you could also pass the parameters, like centx, centy, etc, without having to use global variables.
Also please include the pointmass_derivatives function in the question, as that is an important part of how the results are calculated.

Sign in to comment.

Answers (0)

Categories

Find more on Chemical Spectroscopy in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!