I need help with Matlab

function [y] = hwk1prob1(x)
hwk1prob1= @(x)[tanh(2*x(1)-3*x(2)^2)+1;
exp(.3*x(1)+.4*x(2))-4.7];
y = fsolve( myhwk1prob1,x)
end
This is the code I have written for solving this problem yet I keep runnig into errors such as not enough input arguments, undefined function. I am really new to matlab so any help would be greatly appreciated.

Risposte (2)

Star Strider
Star Strider il 10 Feb 2019
The problem is that you named your parent function and your anonymous function the same. That’s going to cause problems.
Try this slightly edited version:
function [y] = hwk1prob1(x)
hwk1prob1fcn = @(x)[tanh(2*x(1)-3*x(2)^2)+1;
exp(.3*x(1)+.4*x(2))-4.7];
y = fsolve(hwk1prob1fcn,x);
end
x = [42; 42];
y = hwk1prob1(x) % Function Call

5 Commenti

Patrick Potts
Patrick Potts il 10 Feb 2019
Modificato: Patrick Potts il 10 Feb 2019
I tried using your code instead and it still came up with the same issues not enough input arguments, undefined function. So I am at a loss of words since I am just a novice with matlab and it is extremely frustrating to deal with.
The code I posted ran for me without error. I would not have posted it otherwise.
It produced:
y =
10.5195
-4.0207
Walter Roberson
Walter Roberson il 10 Feb 2019
The two lines at the bottom, x = [42; 42]; y = hwk1prob1(x); should go outside of the hwk1prob1.m file . They are showing how to execute the code.
However... your assignment is to create a hwk1prob1.m that does not expect any input, and which determines its own x value to pass as the second parameter to fsolve()
Patrick Potts
Patrick Potts il 10 Feb 2019
I just wanted to make sure there was nothing wrong with my matlab so the values came out just fine. Thanks for all your help.
Star Strider
Star Strider il 11 Feb 2019
If my Answer helped you solve your problem, pleae Accept it!

Accedi per commentare.

Walter Roberson
Walter Roberson il 10 Feb 2019

0 voti

You are asked to write a program hwk1prob1.m . It is not asked to be a function . No input will be passed to it.
Your code needs to try several different x values to find one that works.
Numerically, your function has solutions because of round-off error. Mathematically at least one of the two inputs needs to be infinite for there to be a solution. This is partly because the code shown for fun1 does not implement the equation in the description. Look at where the 1 is relative to the tanh() call: in the equation it is inside the tanh() but in the code it is outside the tanh() . This makes a large difference in the meaning of the equation.

2 Commenti

Patrick Potts
Patrick Potts il 10 Feb 2019
If that is the case could you show me how to get started with matlab because I am just a beginner with what you said in mind because I am still confused?
x0 = SomethingThatYouWillHaveToFigureOut
fsolve(@fun1, x0)

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Richiesto:

il 10 Feb 2019

Commentato:

il 11 Feb 2019

Community Treasure Hunt

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

Start Hunting!

Translated by