|
"Yvonne " <hwt3@waikato.ac.nz> wrote in message <he4e3r$bol$1@fred.mathworks.com>...
> Hi,
>
> I am trying to solve simulataneous equations to create a curve by plugging in the variables a and b into the equation Sscaled = a*Sraw + b;
>
> HT = [32.9 32.4 34.8 35.6];
> LT = [19.8 26.6 29.5 30.3];
>
> The equations to solve a and b is
> a = - (b/LT);
> b= 1/((HT*(a))+b;
>
> How can I solve the equations with two unknowns simultanously to plug into the Sscaled = a*Sraw +b;
>
> Thank you
Hello Yvonne,
I have built a custom solution for you that firstly solves the simultaneous equations
you mentioned and then plots them on a graph. You had an extra left bracket in your second equation above and I was initially unsure as to whether b was in the denominator but quickly realised it must be to make sense mathematically otherwise it would equal zero once terms were collected and would only have had one unknown with no solution other than infinity. That said I rewrote the equations so they were equal to constants with all terms collected.
A chunk of my code (see below) reveals the equations in string format so they
can be used with the Symbolic Math Toolbox 'solve' command.
%build equations
eq1 = '((1-b^2)/b)/a = HT';
eq2 = '-b/a = LT';
I used a for loop to pass all the LT and HT vector constants through the equations
and came up with a,b pair solutions. I then built graphs by plugging in the variables a and b into the equation Sscaled = a*Sraw + b with a nested for loop approach. I used a 3D matrix to hold the coordinates.
I have pasted the code below and submitted it to file exchange along with a HTML
file demonstrating it running. Note I used a script which clears memory and wipes
the command-line window prior to running. You can delete these if they are
inconvenient.
I can be emailed on my gmail email if you require further help or explanation.
Regards
Bruce R.
------------------------------------------------------------------------------------------------------
clear all; clc;
%set up row vectors
HT=[32.9 32.4 34.8 35.6];
LT=[19.8 26.6 29.5 30.3];
%iterate through the vectors
for i=1:4
%build equations
eq1 = '((1-b^2)/b)/a = HT';
eq2 = '-b/a = LT';
%substitute vector values in each algebraic equation
eq1 = subs(eq1,'HT',HT(i));
pretty(eq1);
eq2 = subs(eq2,'LT',LT(i));
pretty(eq2);
%solve simultaneous equations for a and b
s=solve(eq1,eq2,'a,b');
s=[simplify(s.a) simplify(s.b)]; %solution set
s= [eval(s(1)) eval(s(3)); eval(s(2)) eval(s(4))];
allabs(i,1:2) = [s(1) s(3)]; %store first solution of each ab soln set.
disp(s);
disp(allabs);
end;
% 4 sets of x,y values for 4 graphs
for row=1:4
%select arbitrary scale of 0 to 100
for Sraw=0:100
plane=row;
%calculate the y value, Sscaled, from the x value, Sraw
%using a 3D matrix to store everything.
Sscaled(Sraw+1,1:2,plane) = [Sraw allabs(row,1)*Sraw + allabs(row,2)];
end;
end;
%disp(Sscaled);
%plot the Sscaled results making 4 graphs
plot(Sscaled(:,1,1),Sscaled(:,2,1),':r.');
hold on;
plot(Sscaled(:,1,2),Sscaled(:,2,2),':g.');
plot(Sscaled(:,1,3),Sscaled(:,2,3),':b.');
plot(Sscaled(:,1,4),Sscaled(:,2,4),':y.');
xlabel('Sraw')
ylabel('Sscaled')
title('Plot for Yvonne')
|