필터 지우기
필터 지우기

How to use FSOLVE with multiple variables?

조회 수: 31 (최근 30일)
Luize
Luize 2024년 8월 7일 15:04
댓글: Luize 2024년 8월 7일 16:34
Im trying to solve a problem that involves multiple variables and a paramether that chances on every interection.
If I try to run my main file appers this error:
error: 'x' undefined near line 40, column 37
error: called from
main at line 40 column 17
If someone could helps me with this problem I´ll be so greatfull.
>>
My code:
%%%%%%file 1 (main)
%fixed value
global M
M = 33180;
L = leitura_arq('arquivo1.txt', M);
%h = 0.0001;
chute_inicial = [1,1];
% resolve sistem of equations
[x, fval, info] = fsolve (@equacoes(x, M, L), chute_inicial)
%%% FILE 2 (equacoes)
%file used to describe the equations
function F = equacoes (x, M, L)
%valores fixos
M = 33180;
%L = leitura_arq('arquivo1.txt', M);
%x(1) = Betha
%x(2) = Lambda
F(1) = (x(1) * x(2) * M) - (x(1) * (1 / x(2))^(x(1) - 1) * somatorio1(L, x(1)));
F(2) = (M * log(1 / x(2))) + (x(2) * M) + log(sum(L)) - log(x(1));
end
%%%FILE 3 (leitura_arq)
% this file read a csv file and save these values
function L = leitura_arq(arquivo1, M)
L = zeros(1, M); % Inicializa o vetor com zeros
fid = fopen(arquivo1, 'r');
if fid == -1
error('Erro na abertura do arquivo.');
else
disp('Arquivo aberto com sucesso.');
for i = 1:M
L(i) = fscanf(fid, '%f', 1);
end
fclose(fid);
end
end
%%% FILE 4 (somatorio1)
% this file do the interection and sum
function s1 = somatorio1(L, betha)
s1 = sum(L .^ betha);
end

답변 (1개)

Steven Lord
Steven Lord 2024년 8월 7일 15:23
That doesn't look like an error message that would come from MATLAB or Optimization Toolbox. In addition, your code is not syntactically valid. This line will throw an error if you try to run it in MATLAB.
[x, fval, info] = fsolve (@equacoes(x, M, L), chute_inicial)
Is that line a typo or are you using something like Octave rather than MATLAB?
Examining the code I have a couple other suggestions and/or cautions.
If you were running this in MATLAB your approach is close to the anonymous function approach for parameterizing functions.
[x, fval, info] = fsolve (@(x) equacoes(x, M, L), chute_inicial)
I would comment out or remove the following line in your equacoes function, as it overwrites the additional parameter M that you passed into equacoes (with the same value that was passed in as the additional parameter.)
M = 33180;
This line is also not necessary in your main since you're passing M into equacoes as an additional parameter.
global M
In equacoes, you should probably preallocate F to be a column vector or use concatenation rather than assignment to create it. Either:
F = zeros(2, 1);
before the F(1) = and F(2) = lines or:
F = [(x(1) * x(2) * M) - (x(1) * (1 / x(2))^(x(1) - 1) * somatorio1(L, x(1)));
(M * log(1 / x(2))) + (x(2) * M) + log(sum(L)) - log(x(1))];
I'd call fopen in leitura_arq with two outputs.
[fid, osMessage] = fopen(arquivo1, 'r');
This way if opening the file fails, you can include the message from the OS about why opening the file failed as part of your error. This may aid the user in determining what they need to change to allow the file opening to succeed.
Finally, you might need to be careful to avoid the scenarios where the elements of x become non-positive, as you're taking the log of both x(1) and 1/x(2). Maybe use the square of the elements in x in your code instead, so that if they become negative you can still take small steps and get values that are 1) reasonable and 2) continuous.
  댓글 수: 1
Luize
Luize 2024년 8월 7일 16:34
yes, I am using Octave instead os Matlab because my computer could not support matlab. Is is affect on something in my code?
Thanks for the answer, I willl try to do the changes sugested

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by