make code more rigorous

1 次查看(过去 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