Solve MATLAB fsolve Equation: x-(r/4)*sin(pi*x)=0

  • Thread starter Thread starter krnhseya
  • Start date Start date
  • Tags Tags
    Matlab
Click For Summary

Discussion Overview

The discussion revolves around solving the equation x - (r/4)*sin(pi*x) = 0 using MATLAB's fsolve function, with r varying from 0 to 4. Participants are exploring how to implement this in MATLAB, particularly focusing on the requirements of fsolve regarding the number of equations and variables.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes their initial approach to using fsolve, noting the challenge of needing two equations for the function while having only one equation to solve.
  • Another participant expresses frustration at the simplicity of the problem but their inability to find a solution, indicating a lack of clarity in MATLAB examples.
  • A later post presents a modified attempt where the participant defines r as a constant outside the loop and suggests creating a new function that incorporates r as a specific number for each iteration.
  • There is a suggestion that fsolve may not accept a multivariable function directly, prompting discussion about structuring the function appropriately.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the best approach to implement the solution in MATLAB, with multiple competing views on how to structure the function and handle the variable r.

Contextual Notes

Participants mention limitations related to the requirements of fsolve, including the need for multiple equations and the handling of variables within the function. There are also unresolved questions about the correct implementation of the function in MATLAB.

krnhseya
Messages
102
Reaction score
0
I think I posted in a wrong forum...I wasn't sure which category I was supposed to post this under but here it goes...

1. Homework Statement

Find x where r varies from 0 to 4.

2. Homework Equations

x-(r/4)*sin(pi*x)=0

3. The Attempt at a Solution

The problem that I ran into with MATLAB is that when I create a sub-m file for fsolve, it requires 2 equations whereas I only have 1 equation.

function F=myfun(x,r)
F(1)=x-(r/4)*sin(pi*x);

My executable is:

var_new=[];
for r=0:0.01:4;
var=fsolve(@myfun,0.5)
var_new=[var_new var]
i=i+1;
end

I expect to get "var" variable to be a single number where my "relevant equation" is set to zero. var_new is simply creating an array for me to plot r versus var.

Thank you.
 
Physics news on Phys.org
So 1 equation and 1 variable...Looks darn easy but I just can't figure this out and MATLAB examples that I can find (including built-in help) doesn't get me any more help...Anyone...?
 
My new attempt...

i=1;
new_x=[];
newnewx=[];

for r=0.5:0.01:4;
new_x(i)=fsolve(@myfun,[0 r]);
newnewx(i)=((r*pi)/4)*cos(pi*new_x(i));
i=i+1;
end
r=0.5:0.01:4;

plot(r,newnewx)

where the myfun is:

function F=myfun(x)
x=x(1);
r=x(2);
F(1)=x-((r/4)*sin(pi*x));
 
What you can do is define r outside of the loop, then loop through a variable i that goes from 1:length(r). Inside of the loop define the function F so that r is not a variable but a constant for each iteration.

But if you need to have the function in a separate script you can try and create a new function F2(x) = F(x,r) where r is some specific number. I don't think fsolve will just accept a multivariable function.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K