Solve for x: MATLAB Coding Question Homework

In summary: Your Name]In summary, the poster is having trouble using the fsolve function in MATLAB to solve a single equation for a varying parameter. They are trying to create a sub-m file for fsolve, but are unsure how to do so with only one equation. They have attempted to create an executable to solve the equation for different values of r, but are getting an array instead of a single value. The solution is to pass r as a parameter to the myfun function and to assign the output of the function to a single variable.
  • #1
krnhseya
103
0

Homework Statement



Find x where r varies from 0 to 4.

Homework Equations



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

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

Thank you for posting your question. I understand that you are having trouble using the fsolve function in MATLAB to solve a single equation for a varying parameter. I have reviewed your code and it looks like you are on the right track, but there are a few things that can be adjusted to make it work.

Firstly, the fsolve function requires two inputs - the function to be solved and an initial guess for the solution. In your case, you can set the initial guess to be any value since you are iterating through different values of r. So, your code should look like this:

var_new = []; %initialize empty array
for r = 0:0.01:4
var = fsolve(@(x) myfun(x,r), 0.5); %pass r as a parameter to myfun
var_new = [var_new var]; %append var to var_new array
end

Secondly, in your myfun function, you need to assign the output to a single variable, not a vector. So, your code should look like this:

function F = myfun(x,r)
F = x - (r/4)*sin(pi*x); %assign output to F, not F(1)

I hope this helps! Let me know if you have any further questions or if you need any clarification.


 

Related to Solve for x: MATLAB Coding Question Homework

1. What is MATLAB coding and what is it used for?

MATLAB (Matrix Laboratory) is a programming language and interactive environment specifically designed for numerical computation, data visualization, and algorithm development. It is commonly used in scientific and engineering fields for tasks such as data analysis, modeling, and simulation.

2. How do I write and run code in MATLAB?

To write and run MATLAB code, you can use the MATLAB Editor, which is a built-in tool for writing, running, and debugging code. You can also run code through the Command Window by typing in commands or scripts, or by creating and running functions in the Editor.

3. What is the difference between a script and a function in MATLAB?

A script is a file that contains a series of MATLAB commands, while a function is a file that contains a specific set of instructions that can be called and executed by other parts of the code. Functions are typically used for organizing and reusing code, while scripts are useful for quick and simple tasks.

4. How can I learn MATLAB coding?

There are many resources available for learning MATLAB coding, including online tutorials, documentation, and courses. MATLAB also offers a "Getting Started" guide within the program to help beginners learn the basics. Additionally, practicing by writing and running code on your own can greatly improve your skills.

5. Can MATLAB be used for machine learning and artificial intelligence?

Yes, MATLAB has a variety of tools and functions specifically designed for machine learning and artificial intelligence tasks. This includes built-in algorithms for data preprocessing, feature selection, and model training, as well as tools for creating and deploying deep learning models.

Similar threads

  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
12
Views
424
  • Introductory Physics Homework Help
Replies
26
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Introductory Physics Homework Help
Replies
5
Views
3K
  • Introductory Physics Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
782
  • Introductory Physics Homework Help
Replies
5
Views
650
  • Introductory Physics Homework Help
Replies
2
Views
439
Back
Top