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?
T=15
RH=0.4
Td= dewpoint(T, RH)
T=35
RH=0.8
Td= dewpoint(T, RH)
figure
T=25;
RH=0.1:0.05:0.95;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
T=(32-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
T=(77-32).*5./9;
Td= dewpoint(T, RH);
my_plots(Td, RH);
hold on
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)
a = 17.27;
b = 237.7;
Td= zeros(1, length(RHS));
f=zeros(1, length(RHS));
for i=1:length(RH)
f(i) = a.*T./(b + T) + log(RH(i));
Td(i) = b.*f(i)./(a-f(i));
end
end
function p=my_plots(Td, RH)
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.
Best Answer