My matlab code is only returning first output

6 ビュー (過去 30 日間)
Oskar Strömberg
Oskar Strömberg 2020 年 4 月 23 日
コメント済み: Stephen23 2020 年 4 月 23 日
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 件のコメント
Oskar Strömberg
Oskar Strömberg 2020 年 4 月 23 日
That's exactly what I did though...
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by