How to use FSOLVE with multiple variables? (2024)

26 views (last 30 days)

Show older comments

Luize on 7 Aug 2024 at 15:04

  • Link

    Direct link to this question

    https://jmaab.mathworks.com/matlabcentral/answers/2143734-how-to-use-fsolve-with-multiple-variables

  • Link

    Direct link to this question

    https://jmaab.mathworks.com/matlabcentral/answers/2143734-how-to-use-fsolve-with-multiple-variables

Commented: Luize on 7 Aug 2024 at 16:34

Open in MATLAB Online

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;

% 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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Steven Lord on 7 Aug 2024 at 15:23

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/2143734-how-to-use-fsolve-with-multiple-variables#answer_1495994

  • Link

    Direct link to this answer

    https://jmaab.mathworks.com/matlabcentral/answers/2143734-how-to-use-fsolve-with-multiple-variables#answer_1495994

Open in MATLAB Online

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 Comment

Show -1 older commentsHide -1 older comments

Luize on 7 Aug 2024 at 16:34

Direct link to this comment

https://jmaab.mathworks.com/matlabcentral/answers/2143734-how-to-use-fsolve-with-multiple-variables#comment_3232249

  • Link

    Direct link to this comment

    https://jmaab.mathworks.com/matlabcentral/answers/2143734-how-to-use-fsolve-with-multiple-variables#comment_3232249

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

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABProgramming

Find more on Programming in Help Center and File Exchange

Tags

  • fsolve
  • multiplevariables

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to use FSOLVE with multiple variables? (4)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to use FSOLVE with multiple variables? (2024)
Top Articles
Frost Uncut Season 2
Pellissippi Bookstore Hours
Drury Inn & Suites Bowling Green
Patreon, reimagined — a better future for creators and fans
Crocodile Tears - Quest
Unraveling The Mystery: Does Breckie Hill Have A Boyfriend?
Here's how eating according to your blood type could help you keep healthy
Cosentyx® 75 mg Injektionslösung in einer Fertigspritze - PatientenInfo-Service
Encore Atlanta Cheer Competition
R Tiktoksweets
2016 Hyundai Sonata Price, Value, Depreciation & Reviews | Kelley Blue Book
Idaho Harvest Statistics
Craiglist Tulsa Ok
Xomissmandi
Georgia Vehicle Registration Fees Calculator
360 Tabc Answers
Sec Baseball Tournament Score
Craigslist Roseburg Oregon Free Stuff
Silky Jet Water Flosser
Inkwell, pen rests and nib boxes made of pewter, glass and porcelain.
Craigslist Dubuque Iowa Pets
6892697335
Student Portal Stvt
Villano Antillano Desnuda
Xxn Abbreviation List 2017 Pdf
Temu Seat Covers
Craftybase Coupon
Where to eat: the 50 best restaurants in Freiburg im Breisgau
ATM, 3813 N Woodlawn Blvd, Wichita, KS 67220, US - MapQuest
How Do Netspend Cards Work?
Rlcraft Toolbelt
Have you seen this child? Caroline Victoria Teague
Craigslist Central Il
Beth Moore 2023
Austin Automotive Buda
450 Miles Away From Me
Troy Gamefarm Prices
7543460065
Sunrise Garden Beach Resort - Select Hurghada günstig buchen | billareisen.at
Überblick zum Barotrauma - Überblick zum Barotrauma - MSD Manual Profi-Ausgabe
Electronic Music Duo Daft Punk Announces Split After Nearly 3 Decades
2Nd Corinthians 5 Nlt
Booknet.com Contract Marriage 2
The Complete Uber Eats Delivery Driver Guide:
Bonecrusher Upgrade Rs3
Lesson 5 Homework 4.5 Answer Key
Yosemite Sam Hood Ornament
Sams Gas Price San Angelo
The Hardest Quests in Old School RuneScape (Ranked) – FandomSpot
Aaca Not Mine
Taterz Salad
Fishing Hook Memorial Tattoo
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6506

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.