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;
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! :)
댓글 수: 0
채택된 답변
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
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!