Too many output arguments.

2 Ansichten (letzte 30 Tage)
Paul Rogers
Paul Rogers am 16 Dez. 2019
Bearbeitet: per isakson am 18 Dez. 2019
I've found this code that may help me a lot.
I cannot run, it keeps saying:
Error using type
Too many output arguments.
my version is 2014b
the code is
function [t,y]=valori(type)
a=340;
S=0.25;
x10=0.85;
U=144;
if (type == 1)
Ac=0.0034;
Vp=0.32;
Lc=0.3;
H=3.2613e4;
W=0.3689;
else
if (type == 2)
Ac=0.0033;
Vp=0.025;
Lc=1.22;
H=0.09e5;
W=0.1;
else
Ac=0.0090;
Vp=0.001;
Lc=2.8;
H=1.55e4;
W=0.1587;
end
end
[t,y]=greitzer2(a,Ac,Vp,Lc,U,H,W,x10,S);
end
function [t,y]=greitzer2(a,Ac,Vp,Lc,U,H,W,x10,S)
wh=a*sqrt(Ac/(Vp*Lc));
B=U/(2*wh*Lc);
k=x10-S*(x10-(1/3)*(x10.^3));
[t,y]=ode45(@(t,y)greitzer10(H,W,B,U,Ac,y,S,k),[0 100],[0,0]);
subplot(2,2,1)
plot(t,y(:,1))
xlabel('t')
ylabel('\phi_c')
subplot(2,2,2)
plot(y(:,1),y(:,2))
xlabel('\phi_c')
ylabel('\psi')
subplot(2,2,3)
plot(t,y(:,2))
xlabel('t')
ylabel('\psi')
end
function dydt = greitzer10(H,W,B,U,Ac,y,S,k)
dydt = [ ((3*H*Ac*B)/(W*U))*((y(1)-(1/3)*((y(1).^3)))-y(2)); ((W*U)/(3*H*Ac*B))*(y(1)-(S*y(2)+k)) ];
end
this might be a missing part:
function y=carcomp(C0,H,x,W)
y=C0+H*(1+(3/2)*((x/W)-1)-(1/2)*(((x/W)-1).^3));

Akzeptierte Antwort

per isakson
per isakson am 17 Dez. 2019
Bearbeitet: per isakson am 17 Dez. 2019
I fail to reproduce the error you report
>> [t,y] = valori( 2 );
>> [t,y] = valori( 1 );
>> [t,y] = valori( 3 );
no error, no warning and produces a figure with three diagrams.
However
>> [t,y] = valori( type );
Error using type
Too many output arguments.
>>
"type" is the name of a function that doesn't return any output. Try
>> type=1; [t,y] = valori( type );
That works, but now you'll get unexpexted results if you try to use the function, type
Conclusion: Avoid to use function names as names of variables.
  4 Kommentare
Paul Rogers
Paul Rogers am 17 Dez. 2019
function [t,y]=valori(type)
a=340;
S=0.25;
x10=0.85;
U=144;
if (type == 1)
Ac=0.0034;
Vp=0.32;
Lc=0.3;
H=3.2613e4;
W=0.3689;
else
if (type == 2)
Ac=0.0033;
Vp=0.025;
Lc=1.22;
H=0.09e5;
W=0.1;
else
Ac=0.0090;
Vp=0.001;
Lc=2.8;
H=1.55e4;
W=0.1587;
end
end
[t,y]=greitzer2(a,Ac,Vp,Lc,U,H,W,x10,S);
end
function [t,y]=greitzer2(a,Ac,Vp,Lc,U,H,W,x10,S)
wh=a*sqrt(Ac/(Vp*Lc));
B=U/(2*wh*Lc);
k=x10-S*(x10-(1/3)*(x10.^3));
[t,y]=ode45(@(t,y)greitzer10(H,W,B,U,Ac,y,S,k),[0 100],[0,0]);
subplot(2,2,1)
plot(t,y(:,1))
xlabel('t')
ylabel('\phi_c')
subplot(2,2,2)
plot(y(:,1),y(:,2))
xlabel('\phi_c')
ylabel('\psi')
subplot(2,2,3)
plot(t,y(:,2))
xlabel('t')
ylabel('\psi')
end
function dydt = greitzer10(H,W,B,U,Ac,y,S,k)
dydt = [ ((3*H*Ac*B)/(W*U))*((y(1)-(1/3)*((y(1).^3)))-y(2)); ((W*U)/(3*H*Ac*B))*(y(1)-(S*y(2)+k)) ];
end
function y=carcomp(C0,H,x,W)
y=C0+H*(1+(3/2)*((x/W)-1)-(1/2)*(((x/W)-1).^3));
end
C0 = 0; % or a better value
cc = carcomp( C0, H, t, W );
subplot(2,2,4)
plot( t, cc )
xlabel('t')
ylabel('carcomp')
then i type in the command
type=1; [t,y] = valori( type );
but it says this:
Error: File: valori.m Line: 63 Column: 1
This statement is not inside any function.
(It follows the END that terminates the definition of the function "carcomp".)
per isakson
per isakson am 18 Dez. 2019
Bearbeitet: per isakson am 18 Dez. 2019
"This statement is not inside any function." Why did you put it at the end of the file?
Read up on function and Local Functions
Here is a screen clip of the end of your file valori.m
Comments
  • Upper left corner. With an indention of 4, I find it easier to see the scope of the functions.
  • Upper right corner. The red box shows that the Code Analyzer has spotted a syntax error. Try to understand and fix that problem before running the code. See Check Code with the Code Analyzer
  • The arrow points at the the end of the function valori.
Move the lines 61-66 to the end of the function valori.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by