make code more rigorous

조회 수: 2 (최근 30일)
Muhammad Choudhury
Muhammad Choudhury 2022년 2월 10일
답변: David Hill 2022년 2월 10일
I have two seperate functions, fR1 AND fR2 however i would like to combine it into one function as i have to seperature function files.
currently i have this code to calculate rho1 and rho2
Any idea on how to combine it and make it more efficient
clear;clc
L = 20
A = 230
dT =[100:1:120]
a = 0.0039
rho1 = 0.0178
rho2 = 0.0170
a2 = 4.3e-3
b2 = 0.6e-6
rho = rho1*(1+a*dT)
rhoo = rho2*(1+a*dT+b2*(dT).^2)
R1 = fR1(rho,L,A)
R2 = fR2(rhoo,L,A)
hold on
plot(dT,R1)
plot(dT,R2)
xlabel('Temperature')
ylabel('Resistivity')
legend('')
the 2 function filed i have are essential the same thing:
fR1
function R = fR1(rho,L,A)
R = rho*L/A
end
fR2
function R = fR2(rhoo,L,A)
R = rhoo*L/A
end

답변 (1개)

David Hill
David Hill 2022년 2월 10일
Why have functions if you only call them once?
L = 20
A = 230
dT =[100:1:120]
a = 0.0039
rho1 = 0.0178
rho2 = 0.0170
a2 = 4.3e-3
b2 = 0.6e-6
rho = rho1*(1+a*dT)
rhoo = rho2*(1+a*dT+b2*(dT).^2)
R1 = rho*L/A;
R2 = rhoo*L/A;
hold on
plot(dT,R1)
plot(dT,R2)
xlabel('Temperature')
ylabel('Resistivity')
legend('')

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by