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

  • Thread starter krnhseya
  • Start date
  • Tags
    Matlab
In summary, the conversation is about finding the value of x for a given equation where r varies from 0 to 4. The speaker is struggling with using MATLAB and creating a sub-m file for fsolve, as it requires 2 equations but they only have 1. They share their attempt at a solution but are not able to get the desired result. The expert suggests defining r outside of the loop or creating a new function to solve the problem.
  • #1
krnhseya
103
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
  • #2
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...?
 
  • #3
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));
 
  • #4
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.
 
  • #5




Thank you for your post. Based on the information provided, it seems like you are trying to use the fsolve function in MATLAB to solve a single equation with one variable (x) and a parameter (r). The fsolve function in MATLAB is typically used to solve systems of equations, which is why it requires two equations as input. However, it is still possible to use fsolve to solve a single equation with one variable and a parameter by modifying the input arguments.

One way to do this is to define your equation as a function of only one variable (x) and use an anonymous function to specify the parameter (r). For example, you can define your equation as follows:

myfun = @(x) x - (r/4)*sin(pi*x);

Then, you can use this function in the fsolve command as follows:

var = fsolve(myfun, 0.5);

This will give you the value of x that satisfies the equation for a given value of r. You can then use a loop to vary the value of r and solve for x at each iteration, as shown in your example code.

I hope this helps. Let me know if you have any further questions.
 

1. What is fsolve in MATLAB?

fsolve is a function in MATLAB that is used for finding the roots of a given equation or system of equations. It uses a numerical method called the Newton-Raphson method to iteratively approximate the root of the equation.

2. How do I use fsolve in MATLAB?

To use fsolve in MATLAB, you need to provide the function that represents the equation you want to solve, along with an initial guess for the root. You can also specify additional options such as the maximum number of iterations and the tolerance for the approximate solution.

3. What is the syntax for using fsolve in MATLAB?

The syntax for using fsolve in MATLAB is:
[x, fval, exitflag, output] = fsolve(fun, x0, options)
Where "fun" is the function representing the equation, "x0" is the initial guess, and "options" are the additional options specified.

4. How does fsolve find the solution to an equation?

fsolve uses the Newton-Raphson method, which is an iterative process that starts with an initial guess and then updates the guess using the derivative of the function at that point. This process is repeated until the approximate solution converges within the specified tolerance.

5. Can fsolve solve any type of equation?

fsolve can solve most types of equations, including nonlinear equations and systems of equations. However, it may not always find a solution, especially if the function is discontinuous or has multiple roots. It is important to provide a good initial guess and to check the results for accuracy.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
807
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
855
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
823
Replies
4
Views
347
Back
Top