My matlab code is only returning first output

조회 수: 4 (최근 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 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