How to plot function with different first x values?

2 次查看(过去 30 天)
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! :)

采纳的回答

Torsten
Torsten 2024-3-13
编辑:Torsten 2024-3-13
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 个评论
Walter Roberson
Walter Roberson 2024-3-13
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 2024-3-13
移动:Torsten 2024-3-13
Wou thank you so much for the helpful and quick responses @Torsten and @Walter Roberson! You saved my day!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by