It either tells me "Too many arguments specify one" or "index in position 2exceeds array bounds" for line 9 (plot)

3 ビュー (過去 30 日間)
I am using the following code:
to solve a simulink question I have models and all of them give me error saying it's from the code. what can I do to make it run?
clear
clc
close all
tau=4.904;
for i = 1:5
PI = [10.7,11,11.5,11.8,12];
Kc = PI(1,i);
[t,x,y] = sim('q3modelgroup3_1',15);
plot(t,y(:,1));
xlabel('Time');
ylabel('X''(t)');
legend('Kc = 10.7','Kc = 11','Kc = 11.5','Kc = 11.8','Kc = 12');
title('Composition VS Time');
xlim([0 15]);
hold on
end
Too many return arguments are specified. Specify only one.
hold off

回答 (1 件)

Image Analyst
Image Analyst 2023 年 6 月 4 日
Evidently sim does not produce 3 output arguments. You seem to think it will give you t, x, and y. It does not.
help sim
SIM Simulate a Simulink model SimOut = SIM('MODEL', PARAMETERS) simulates your Simulink model, where 'PARAMETERS' represents a list of parameter name-value pairs, a structure containing parameter settings, or a configuration set. The SimOut returned by the SIM command is an object that contains all of the logged simulation results. Optional PARAMETERS can be used to override existing block diagram configuration parameters for the duration of the simulation. This syntax is referred to as the 'Single-Output Format'. SINGLE-OUTPUT FORMAT -------------------- SimOut = SIM('MODEL','PARAMETER_NAME1',VALUE1,'PARAMETER_NAME2',VALUE2, ...) SimOut = SIM('MODEL', PARAM_NAME_VAL_STRUCT) SimOut = SIM('MODEL', CONFIGSET) All simulation outputs (logged time, states, and signals) are returned in a single Simulink.SimulationOutput object. Using the model's Configuration Parameters Data Import/Export dialog, you define the model time, states, and output to be logged. You can log signals using blocks such as the To Workspace and Scope blocks. The Signal & Scope Manager can directly log signals. Where: SimOut : Returned Simulink.SimulationOutput object containing all of the simulation output. 'MODEL' : Name of a block diagram model. 'PARAMETER_NAME' : Name of the Configuration or Block Diagram parameter. VALUE : Value of the corresponding Configuration or Block Diagram parameter. PARAM_NAME_VAL_STRUCT : This is a structure whose fields are the names of the block diagram or the configuration parameters that are being changed for the simulation. The corresponding values are the corresponding parameter values. CONFIGSET : The set of configuration parameters for a model. The single-output format makes the SIM command compatible with PARFOR by eliminating any transparency issues. See "Running Parallel Simulations" in the Simulink documentation for further details. Example 1: simOut = sim('vdp','SimulationMode','rapid','AbsTol','1e-5',... 'SaveState','on','StateSaveName','xoutNew',... 'SaveOutput','on','OutputSaveName','youtNew'); simOutVars = simOut.who; yout = simOut.find('youtNew'); Example 2: paramNameValStruct.SimulationMode = 'rapid'; paramNameValStruct.AbsTol = '1e-5'; paramNameValStruct.SaveState = 'on'; paramNameValStruct.StateSaveName = 'xoutNew'; paramNameValStruct.SaveOutput = 'on'; paramNameValStruct.OutputSaveName = 'youtNew'; simOut = sim('vdp',paramNameValStruct); Example 3: mdl = 'vdp'; load_system(mdl); simMode = get_param(mdl, 'SimulationMode'); set_param(mdl, 'SimulationMode', 'rapid'); cs = getActiveConfigSet(mdl); mdl_cs = cs.copy; set_param(mdl_cs,'AbsTol','1e-5',... 'SaveState','on','StateSaveName','xoutNew',... 'SaveOutput','on','OutputSaveName','youtNew'); simOut = sim(mdl, mdl_cs); set_param(mdl, 'SimulationMode', simMode); DEFAULTS: 1. R = SIM('MODEL') returns the result R as either a Simulink.SimulationOutput object or a time vector that is compatible with a Simulink version prior to 7.4 (R2009b). To make SIM('MODEL') return in the single-output format, use the ReturnWorkspaceOutputs option: SimOut = SIM('MODEL', 'ReturnWorkspaceOutputs', 'on') 2. To set the single-output format as the default format, select the 'Return as single output' option on the Data Import/Export pane of the Configuration Parameters dialog box and save the model. See also SLDEBUG, SIM in PARFOR Documentation for sim doc sim Other uses of sim deeplearning/sim idmodel/sim network/sim explicitMPC/sim mpc/sim sdo.SimulationTest/sim iddata/sim
Try
simOut = sim('q3modelgroup3_1', 15);
If that doesn't work, then read the documentation fully and more carefully.
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 6 月 4 日
In particular the Model Adviser recommends switching the model from multiple outputs to single output; https://www.mathworks.com/help/simulink/gui/singlesimulationoutput.html . If you had followed that advice then there would only be a single output and you would have to extract data from the single output.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGeneral Applications についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by