Unable to find error in called function code

5 vues (au cours des 30 derniers jours)
Brandon Yarberry
Brandon Yarberry le 17 Nov 2021
Réponse apportée : Shivam Lahoti le 11 Nov 2024 à 17:53
I'm trying to work out a problem with my code. For the assignment, we are recreating our excel solutions from the previous assignment in MATLAB.
The lfm = lf+lm value is what is supposed to be driven towards 0 through newtons method, returning a value of Vdot = 85.41 (ish, more decimals, some rounding, etc are ok). The way it is set up, the main program should set up and call the lfm_Yarberry_v4 function through the Newton2_Yarberry function, returning the answer. Previous attempts usually gave a value of infinite as the return, but the newest version is giving me complex imaginary numbers. Not sure if that's progress or not.
Troubleshooting attempted:
I have a classmates work that actually works, and have confirmed that my Newton2_Yarberry function works correctly. My main program and the lfm_Yarberry function have been organized to be very close to their functional code, but outside of directly copying their work, I am unable to figure out why my code is not working. I have checked every calculation outside of the functions and the returned values are the same returned from my excel sheet.
My best guess is that I have either named a variable something that MATLAB is interpreting as some inbuilt function, or somewhere a variable is not being properly called or referenced, but for the life of me I cannot figure it out.
Please assist.

Réponses (1)

Shivam Lahoti
Shivam Lahoti le 11 Nov 2024 à 17:53
Hello,
It sounds like you're facing challenges with implementing Newton's method in MATLAB, particularly with unexpected complex numbers in your results. Here are some steps to help troubleshoot and resolve the issue:
  1. Variable Naming: Ensure no variables conflict with MATLAB's built-in functions. Use the which command to verify, e.g., which lf.
  2. Debugging Techniques: Use breakpoints to step through your code and monitor variable values. This helps identify where calculations might go wrong, especially if complex numbers appear from invalid operations.
You might be able to narrow down the issue by following these steps.
Regards,
Shivam

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by