Folsve issues in function definition

I have a system of nonlinear equations and I am trying to solve with the fsolve solver. I defined the function and the variables in the function but I had an error. Attached is the Matlab code of the function. I would be glad for any assistance.

 Accepted Answer

Stephen23
Stephen23 on 6 Aug 2018
Edited: Stephen23 on 6 Aug 2018
Get rid of these two useless lines of code:
clear all
clc
They serve no relevant functional purpose, and just cause you bugs (like this one). The only reason beginners put clear at the start of everything that they write is because of this:
Do not put clear at the start of everything. It does not help: it created this bug. It is not related to your code's functionality. Well written code does not need clear (or atleast very rarely): if all variables are preallcoated (as they should be) and code is written in functions (as it should be) then MATLAB's inbuilt memory manager takes quite good care of allocating and clearing variables from memory.

6 Comments

Thank you so much for the explanation and the term you introduced to me.
Hello sir, I rectified it but it doesn't run after making the correction.I have posted the modified code above.
@Honey Adams: at the top of FIFON_CALL there is a list of variable(?) names: V;W;Z;k;center;left; right;: where are their values defined?
Note that a simpler way to define these:
a=[x(1);x(2);x(3);x(4);x(5);x(6)];
r=[x(7);x(8);x(9);x(10);x(11);x(12)];
s=[x(13);x(14);x(15);x(16);x(17);x(18)];
is using some simple indexing:
x = x(:);
a = x(1:6);
r = x(7:12);
s = x(13:18);
I suspect that you need to use element-wise array operators, rather then the linear algebra matrix operators that you have used. Read this carefully to know the difference:
Then, if you actually need array operators, change * to .*, / to ./, and ^ to .^ as appropriate. Check your code by running it: don't bother trying fsolve until you can run FIFON by itself directly, with the same size arguments that fsolve will provide it with. Compare its output to one you calculate using another method (e.g. by hand, on paper).
I attached a data for the code up there;
Thank you for the link.So i modified it as suggested by the other contributor but it still doesnt run.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!