How to plot function with different first x values?

2 views (last 30 days)
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! :)

Accepted Answer

Torsten
Torsten on 13 Mar 2024
Edited: Torsten on 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 Comments
Walter Roberson
Walter Roberson on 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 on 13 Mar 2024
Moved: Torsten on 13 Mar 2024
Wou thank you so much for the helpful and quick responses @Torsten and @Walter Roberson! You saved my day!

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!