How to plot function with different first x values?

3 vues (au cours des 30 derniers jours)
Jenni
Jenni le 13 Mar 2024
Déplacé(e) : Torsten le 13 Mar 2024
Hey!
I am trying to plot voltage, current and the power. My task is to subplot three plots where are these situations:
Current (I):
When resistance is 0 < R < 10 then current (I) is constant 0,5 A and when 10 < R < 100 then current is 5/R.
Voltage (U):
When resistance is 0 < R < 10 then voltage is linear function 0,5R and when 10 < R < 100 then voltage is constant 5 V.
Power:
U * I
My code is looking like this at the moment, but I am getting message from the matlab that "Vectors must be the same length.":
close all
clear all
u_set = 5;
i_set = 0.5;
rten= 1:1:10;
rhundred = 10:1:100;
r = 1:1:100;
voltage = rten.*(i_set);
current = u_set.*((1./rhundred));
power = voltage.*current;
Arrays have incompatible sizes for this operation.
figure(1)
subplot(3,1,1)
plot(rten,voltage)
hold on
plot(rshundred,yline(5),Color='b')
xlabel('Resistance (Ω)')
ylabel('Voltage (V)')
title('Voltage')
grid on;
subplot(3,1,2),
plot(rvakio,virta)
hold on
plot(rhundred,yline(0.5),Color='b')
xlabel('Resistance (Ω)')
ylabel('Current (A)')
title('Current')
grid on;
subplot(3,1,3),
plot(r,power),
xlabel('Resistance (Ω)')
ylabel('Power (W)')
title('Power')
grid on;
How could I change my code that the arrays would match? Thank you for the help! :)

Réponse acceptée

Torsten
Torsten le 13 Mar 2024
Modifié(e) : Torsten le 13 Mar 2024
I = @(R)0.5*(R>=0 & R<10)+5/R*(R>=10 & R<=100);
U = @(R)0.5*R*(R>=0 & R<10)+5*(R>=10 & R<=100);
P = @(R)I(R)*U(R);
R = 0:0.1:100;
plot(R,arrayfun(@(R)P(R),R))
grid on
  2 commentaires
Walter Roberson
Walter Roberson le 13 Mar 2024
I = @(R) 0.5*(R>=0 & R<10) + 5./R.*(R>=10 & R<=100);
U = @(R) 0.5*R.*(R>=0 & R<10) + 5*(R>=10 & R<=100);
P = @(R)I(R).*U(R);
R = 0:0.1:100;
plot(R,P(R))
grid on
Jenni
Jenni le 13 Mar 2024
Déplacé(e) : Torsten le 13 Mar 2024
Wou thank you so much for the helpful and quick responses @Torsten and @Walter Roberson! You saved my day!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB 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!

Translated by