My matlab code is only returning first output
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Oskar Strömberg
le 23 Avr 2020
Commenté : Stephen23
le 23 Avr 2020
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
0 commentaires
Réponse acceptée
Stephen23
le 23 Avr 2020
Modifié(e) : Stephen23
le 23 Avr 2020
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 commentaires
Stephen23
le 23 Avr 2020
"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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!