Using MatLab to develop an Mfile function to determine the e

Using MatLab to develop an M-file function to determine the elapsed days in a year.

3.16 Develop an M-file function to determine the elapsed days in a year. The first line of the function should be set up as function nd = days (mo, da, leap) where mo the month (1-12), da the day (1-31), and leap (0 for non-leap year and 1 for leap year). Test it for January 1, 1997, February 29, 2004, March 1, 2001, June 21,

Solution

function nd = days(mo,da,leap)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
year=zeros(12,1);
year([1 2 3 4])=[31,28,31,30];
year([5 6 7 8])=[31,30,31,31];
year([9 10 11 12])=[30,31,30,31];
i=1;
nd=0;
while i<mo
    nd=nd+year(i);
    i=i+1;
end
nd=nd+(da-1);
if mo~=2 %to add extra day for leap year
    nd=nd+leap;
end

%%out put

>> days(1,1,0)

ans =

     0

>> days(2,29,1)

ans =

    59

>> days(3,1,0)

ans =

    59

>> days(6,21,1)

ans =

   172

>> days(12,31,1)

ans =

   365

Using MatLab to develop an M-file function to determine the elapsed days in a year. 3.16 Develop an M-file function to determine the elapsed days in a year. The

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site