# Im trying to find the zeroes of the two lines on the graphs I've made

1 view (last 30 days)
Nickolas Marcove on 8 Feb 2022
Answered: Star Strider on 9 Feb 2022
I've looked around a bit and either I haven't been putting in stuff the right way or I haven't found the help I'm looking for. I am making plots for the power a solar collector collects through out a day. I need to find where the lines hit 0 on the x axis so I can determine sunrise and sunset of each line so I can intergrate for the day. The last few lines are a piece I couldn't get to work properly. This is what I have so far:
n = 32;
S = 23.45*sind(360*(284 + n)/365);
phi = 38.9072;
tsol = 0:1/60:24;
w = 15*(tsol-12);
Gsc = 1353;
Go = Gsc*(1 + 0.033*cosd((360*n)/365))*(cosd(phi)*cosd(S)*cosd(w) + sind(phi)*sind(S));
plot(tsol,Go)
hold on
Y = 30;
B = 50;
theta1 = sind(S)*sind(phi)*cosd(B) - sind(S)*cosd(phi)*sind(B)*cosd(Y) + cosd(S)*cosd(phi)*cosd(B)*cosd(w)+ cosd(S)*sind(phi)*sind(B)*cosd(Y)*cosd(w) + cosd(S)*sind(B)*sind(Y)*sind(w);
Got = Gsc*(1 + 0.033*cosd((360*n)/365))*theta1;
plot(tsol,Got)
hold off
grid on
title('Homework #3, Question 1b')
xlabel('Time of day (Hours)')
xlim([0 24])
legend('Horrizontal','Angled')
syms tsol Go
tsol = solve([0 == Gsc*(1 + 0.033*cosd((360*n)/365))*(cosd(phi)*cosd(S)*cosd(w) + sind(phi)*sind(S))-Go], tsol)

Star Strider on 9 Feb 2022
Another approach —
n = 32;
S = 23.45*sind(360*(284 + n)/365);
phi = 38.9072;
tsol = 0:1/60:24;
w = 15*(tsol-12);
Gsc = 1353;
Go = Gsc*(1 + 0.033*cosd((360*n)/365))*(cosd(phi)*cosd(S)*cosd(w) + sind(phi)*sind(S));
plot(tsol,Go)
hold on
Y = 30;
B = 50;
theta1 = sind(S)*sind(phi)*cosd(B) - sind(S)*cosd(phi)*sind(B)*cosd(Y) + cosd(S)*cosd(phi)*cosd(B)*cosd(w)+ cosd(S)*sind(phi)*sind(B)*cosd(Y)*cosd(w) + cosd(S)*sind(B)*sind(Y)*sind(w);
Got = Gsc*(1 + 0.033*cosd((360*n)/365))*theta1;
plot(tsol,Got)
Go_idx = find(diff(sign(Go))); % Approximate Indices
for k = 1:numel(Go_idx)
idxrng = max([1,Go_idx(k)-1]) : min([numel(Go),Go_idx(k)+1]); % Index Range For Interpolation
Go_t(k) = interp1(Go(idxrng), tsol(idxrng), 0); % Precise Values
end
Got_idx = find(diff(sign(Got))); % Approximate Indices
for k = 1:numel(Got_idx)
idxrng = max([1,Got_idx(k)-1]) : min([numel(Got),Got_idx(k)+1]); % Index Range For Interpolation
Got_t(k) = interp1(Got(idxrng), tsol(idxrng), 0); % Precise Values
end
plot([Go_t Got_t], zeros(1,numel(Go_t)+numel(Got_t)), '+k','MarkerSize',10)
hold off
grid on
title('Homework #3, Question 1b')
xlabel('Time of day (Hours)')
xlim([0 24])
legend('Horrizontal','Angled') syms tsol Go
tsol = solve([0 == Gsc*(1 + 0.033*cosd((360*n)/365))*(cosd(phi)*cosd(S)*cosd(w) + sind(phi)*sind(S))-Go], tsol)
tsol = Empty sym: 0-by-1
.

Turlough Hughes on 8 Feb 2022
If you have the mapping toolbox then polyxpoly is very handy for this. For example:
x = linspace(-1.5*pi,1.5*pi,1000);
y = sin(x);
figure(), plot(x,y);
[xi, yi] = polyxpoly(x,y,x,zeros(size(x)));
hold on, plot(xi,yi,'ok','MarkerFaceColor','k') dpb on 8 Feb 2022
I lack symbolic TB, but it's easy enough to just find the crossing point from linear interpolation. MATLAB interp1 requires a unique sorted variable as its independent, so to do the backwards interpolation to find x given y with a double-valued function, have to segregate the regions -- again, this is easy enough.
Since is HM, I'll only show the result; you're honor-bound to determine how it works and submit your own version... :)
iz1=find(Got>0,1);
iz2=find(Got>0,1,'last');
tZ1=interp1(Got(iz1-1:iz1),tsol(iz1-1:iz1),0);
tZ2=interp1(Got(iz2:iz2+1),tsol(iz2:iz2+1),0)
xline(tZ1,'r:')
xline(tZ2,'r:')

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!