# Help with MATLAB code; discrepancy between graph max and Matlab's max

• MATLAB
• babayevdavid
In summary, the MATLAB code is producing a graph showing the voltage difference between two different calculations. It appears that there is an issue with the maximum voltage difference, as it is significantly larger than the rest. However, upon further investigation, it is revealed that the maximum voltage difference occurs at the expected value of Rf, and the issue lies in how the code is printing the index of Rf instead of the actual value.
babayevdavid
Hi,

For some reason, when I run this MATLAB code, I'm getting what looks like about 10,000 for Rf for the largest Vo on the graph, but MATLAB is giving me Rf at (Vo)max as 100,001

Any ideas?

clc;
clear;
close all;

syms Rf

Rf = 0:0.1:100000;

VRt25 = 18.*(10000)./(10000 + Rf);
VRf25 = 18.*(Rf)./(10000 + Rf);

VRt2505 = 18.*(9977.81)./(9977.81 + Rf);
VRf2505 = 18.*(Rf)./(9977.81 + Rf);

V_difference25 = abs(VRt25 - VRf25);

V_difference2505 = abs(VRt2505 - VRf2505);

Vo_difference = abs(V_difference25 - V_difference2505);

figure;
plot(Rf,Vo_difference)
title('Voltage Difference Vo vs. Rf')
ylabel('Vo Difference [V]')
xlabel('Rf [ohms]')
hold on

%Maximum Vo Difference
[Vo_difference_max,Rf] = max(Vo_difference)

There is no issue here.

Rf has 1,000,001 elements, as does Vo_difference.

The maximum of Vo_difference is 0.02, occurring at index 100,001. This means Vo_difference(100001) = 0.02.

The part you missed is that Rf(100001) = 10000 as expected. That is, the Rf you printed with max() gives you an index _into_ the original Rf, not a direct value of Rf.

## 1. What could be causing a discrepancy between the maximum value shown on my graph and the maximum value calculated by MATLAB?

There are a few possible reasons for this discrepancy. It could be due to rounding errors in your calculations, the way the graph is scaled or labeled, or the presence of outliers in your data. It is important to carefully check your code and make sure all relevant data points are included in your calculations.

## 2. How can I troubleshoot this issue and determine the source of the discrepancy?

One way to troubleshoot this issue is to manually check the data points and compare them to the values shown on the graph. You can also use the debug feature in MATLAB to step through your code and identify any potential errors or inconsistencies.

## 3. Is there a way to adjust the graph settings to match the maximum value calculated by MATLAB?

Yes, you can adjust the graph settings such as the axis limits or the scaling method to better match the maximum value calculated by MATLAB. You can also use the "hold on" command to overlay multiple graphs and more accurately compare the values.

## 4. Could this discrepancy be due to a programming error?

Yes, it is possible that the discrepancy is caused by a programming error. It is important to carefully review your code and make sure all relevant data points are included in your calculations. It may also be helpful to get a second opinion from a colleague or consult online resources for troubleshooting tips.

## 5. How can I prevent this discrepancy from occurring in the future?

To prevent this discrepancy from occurring in the future, it is important to double-check your code and make sure all relevant data points are included in your calculations. It may also be helpful to use more accurate or precise functions in MATLAB, such as "max" instead of "maximize". Additionally, regularly testing and debugging your code can help catch any errors early on.

• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
493
• MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
• MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K