Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Saturday, 7 December 2019

The average monthly precipitation (in.) for Boston and Seattle during 2012 are given in the vectors below

Q: The average monthly precipitation (in.) for Boston and Seattle during 2012 are given in the vectors below (data from the U.S. National Oceanic and Atmospheric Administration) BOS = [2.67 1.00 1.21 3.09 3.43 4.71 3.88 3.08 4.10 2.62 1.01 5.93] SEA = [6.83 3.63 7.20 2.68 2.05 2.96 1.04 0.00 0.03 6.71 8.28 6.85] Where the elements in the vectors are in the order of the months (January, February, etc.) Write your program to answer the following: (a) Calculate the total precipitation for the year and monthly average precipitation in each city. Do NOT use MATLAB's built-in functions sum and mean. (b) How many months was the precipitation above the average in each city? (c) How many months, and on which months, was the precipitation in Boston lower than the precipitation in Seattle? 

Solution:

 
clear all;
close all;
bos=[2.67,1,1.21,3.09,3.43,4.71,3.88,3.08,4.10,2.62,1.01,5.93];
sea=[6.83,3.63,7.20,2.68,2.05,2.96,1.04,0,0.03,6.71,8.28,6.85];
tot_bos=0;
tot_sea=0;
for x=1:1:12
tot_bos=tot_bos+bos(x);
tot_sea=tot_sea+sea(x);
end
avg_bos=tot_bos/12;
avg_sea=tot_sea/12;
above_avg_bos=0;
above_avg_sea=0;
for y=1:1:12
if(bos(y)>avg_bos)
above_avg_bos=1+above_avg_bos
end;
if(sea(y)>avg_sea)
above_avg_sea=1+above_avg_sea
end;
end
i=1;
for z=1:1:12
if(bos(z)<sea(z));
a(i)=z;
i=i+1;
end
end
fprintf('Total participation for the year in BOS ans SEA are %0.2d %0.2d\n',tot_bos,tot_sea);
fprintf('Monthly avg participation for the year in BOS ans SEA are %0.2d %d\n',avg_bos,avg_sea);
fprintf('Totoal month of above avg participation in the year for BOS and SEA %i %i\n',above_avg_bos,above_avg_sea);
fprintf('Total month where BOS is less than SEA %i and months are %i',i-1,a);

The average monthly precipitation (in.) for Boston and Seattle during 2012 are given in the vectors below

Q: The average monthly precipitation (in.) for Boston and Seattle during 2012 are given in the vectors below (data from the U.S. National Oceanic and Atmospheric Administration). BOS = [2.67 1.00 1.21 3.09 3.43 4.71 3.88 3.08 4.10 2.62 1.01 5.93] SEA = [6.83 3.63 7.20 2.68 2.05 2.96 1.04 0.00 0.03 6.71 8.28 6.85] where the elements in the vectors are in the order of the months (January, February, etc.) Write a program to answer the following: Calculate the total precipitation for the year and monthly average precipitation in each city. How many months was the precipitation above the average in each city? (c) How many months, and on which months, was the precipitation in Boston lower than the precipitation in Seattle?

Solution:

 
bos = [2.67 1.00 1.21 3.09 3.43 4.71 3.88 3.08 4.10 2.62 1.01 5.93];
sea = [6.83 3.63 7.20 2.68 2.05 2.96 1.04 0.00 0.03 6.71 8.28 6.85];
fprintf('Total precipitation in Boston=%f \n',sum(bos));
fprintf('Total precipitation in Seattle=%f \n',sum(sea));
fprintf('Average monthly precipitation in Boston=%f \n',sum(bos)/12);
fprintf('Average monthly precipitation in Seattle=%f \n',sum(sea)/12);
numMonthBos=0;
for v=bos
if v > sum(bos)/12
numMonthBos+=1;
end
end
numMonthSea=0;
for v=bos
if v > sum(sea)/12
numMonthSea+=1;
end
end
fprintf('Number of months when precipitation is above average in Boston=%d \n',numMonthBos);
fprintf('Number of months when precipitation is above average in Seattle=%d \n',numMonthSea);
total1=0;
for i=1:12
if bos(i) < sea(i)
total1+=1;
end
end
fprintf('Number of months when precipitation is lower in boston than in Seattle=%d \n',total1);

sh-4.3$ octave -qf --no-window-system demo.m octave: X11 DISPLAY environment variable not set octave: disabling warning: func

Write a program that will collect numbers from the user until the user enters a non-numeric value.

Q:
MATLAB Help:
Write a program that will collect numbers from the user until the user enters a non-numeric value. It then returns the sum and the average of all the numbers entered. Example below:
-----
Enter a number: 2.5
Enter a Number: 2
Enter a Number: 59
Enter a Number: 2
Enter a Number: 9
Enter a Number: 1.1
Enter a Number: k
The sum of the 6 numbers entered is 75.6. The average is 12.6.

Solution:

result = 0;
count = 0;
while(1)
val = input("Enter a number: ",'s')
val = str2double(val)
if(val == NaN)
break;
else
result = result + val;
end
count = count + 1;
end
fprintf("The sum of the %d numbers entered is %f. The average is %f.\n",count,result, (result/count));

Write a program that will collect one positive integer from the user. Validation is required.

Q:
MATLAB Help:
Write a program that will collect one positive integer from the user. Validation is required. Specifications:
1. The program should collect the input as a string and convert it to a numeric number
2. The program will check if the user enters nothing, a non-numeric value, or not a positive integer. It will keep prompting until the user enters a positive integer.
3. Design a prompt/feedback
Functions you may need: isempty(), isnan(), length(), str2double(), str2num()

Solution:


k=1;
while k==1
    k=0;
    num=input('Enter number: ','s');
    if isempty(num)
        k=1;
        fprintf('you did not enter anything\n')
    elseif isnan(str2double(num))
        k=1;
        fprintf('You entered a non numeric value\n')
    elseif str2double(num)<0
        k=1;
        fprintf('Please enter a positive value\n')
    else
        num=str2double(num);
    end
end
fprintf('You entered :%i\n',num)