MATLAB: Their something wrong with the script need help please

script wrong

%the dewpoint function
function Td= dewpoint(T, RH)
%set the a and b values
a = 17.27;
b = 237.7;
%declare the
Td=[];
f=[];
for i=1:length(RH)
%compute f
f(i) = a.*T./(b + T) + log(RH(i));
%copute Td
Td(i) = b.*f(i)./(a-f(i));
end
end
%the my_plot function
function p=my_plots(Td, RH)
%plot and labeling
plot(Td, RH)
title('Mutaz alsomali Dew point plot')
xlabel('Td (F)')
ylabel('RH(%)')
end
%test case1
T=15
RH=0.4
Td= dewpoint(T, RH)
%test case 2
T=35
RH=0.8
Td= dewpoint(T, RH)
figure
%plot for 25
T=25;
RH=0.1:0.05:0.95;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 32F
T=(32-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 77F
T=(77-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 95F
T=(95-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
legend('25 C', '32F', '77F', '95F')
hold off

Best Answer

The error message is clear already and contains the required instructions:
Function definitions in a script must appear at the end of the file.
Move all statements after the "dewpoint" function definition to before
the first local function definition.
Did you try this already?
%test case1
T=15
RH=0.4
Td= dewpoint(T, RH)
%test case 2
T=35
RH=0.8
Td= dewpoint(T, RH)
figure
%plot for 25
T=25;
RH=0.1:0.05:0.95;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 32F
T=(32-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 77F
T=(77-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
%plot for 95F
T=(95-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
legend('25 C', '32F', '77F', '95F')
hold off
function Td = dewpoint(T, RH)
%set the a and b values
a = 17.27;
b = 237.7;
%declare the
Td= zeros(1, length(RHS)); % PRE-ALLOCATE !!!

f=zeros(1, length(RHS)); % PRE-ALLOCATE !!!
for i=1:length(RH)
%compute f
f(i) = a.*T./(b + T) + log(RH(i));
%copute Td
Td(i) = b.*f(i)./(a-f(i));
end
end
%the my_plot function
function p=my_plots(Td, RH)
%plot and labeling
plot(Td, RH)
title('Mutaz alsomali Dew point plot')
xlabel('Td (F)')
ylabel('RH(%)')
end
I've simple moved the script part of the file to the top as explained in the message.
By the way: Try to write meaningful comments. "%set the a and b values" is very obvious, but where do the values come from? "%declare the" is not useful and "%the my_plot function" is neither. Comments will save your live as programmer. While an exhaustively commented code can be expanded, maintained and debugged, missing comments leaves code in a status, in which deleting and rewriting is the most reliable option.
Pre-allocation is much more efficient than letting an array grow iteratively.
Related Question