How to plot function with different first x values?

조회 수: 2 (최근 30일)
Jenni
Jenni 2024년 3월 13일
이동: Torsten 2024년 3월 13일
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