My matlab code is only returning first output

6 次查看(过去 30 天)
So my code here is only returning the first ouput voltage and is also saving it as ans in workshop instead of voltage, I want it to return all three outputs by their name and save them into workshop but I can't figure out what's wrong?
function [voltage, current, evarde] = kretsen2;
U0 = 220;
U = U0;
I0 = 0;
I = I0;
u0 = [I0;U0];
u = u0;
C = 0.5*(10^-6);
L0 = 0.7;
t = 2*pi*sqrt(L0*C);
%t = 0.01;
f = @(t,u) [u(2)/(L0*((1^2/(u(1)^2+1^2))));-u(1)/C];
n = 1000
h = t/n;
listX = 0
listY = u'
for x = 0:h:t-h
k1 = f(x,u);
k2 = f((x+h)/2,u + (h/2)*k1);
k3 = f((x+h)/2,u + (h/2)*k2);
k4 = f(x+h,u + h*k3);
u = u + (h/6)*(k1+2*k2+2*k3+k4);
listX = [listX;x];
listY = [listY;u'];
end
current = listY(:,1);
voltage = listY(:,2);
evarde = (1/2)*C*voltage.^2 + (1/2)*L0*log(1+current.^2);
figure
plot(listX,current)
title("current")
figure
plot(listX,voltage)
title("voltage")
figure
plot(listX,evarde)
title("E(t)")
end

采纳的回答

Stephen23
Stephen23 2020-4-23
编辑:Stephen23 2020-4-23
You need to call the function with all three outputs, e.g.:
[Vout, Iout, E] = kretsen2();
Very basic MATLAB concepts, such as how to call functions with multiple output arguments, are explained in the introductory tutorials:
  2 个评论
Stephen23
Stephen23 2020-4-23
"That's exactly what I did though"
Did it work then? If not, please explain how you are checking if the variables are that workspace.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by